Changeset 15 in flair-src for trunk/lib/FlairCore/src/IODevice_impl.cpp


Ignore:
Timestamp:
04/08/16 15:40:57 (6 years ago)
Author:
Bayard Gildas
Message:

sources reformatted with flair-format-dir script

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/IODevice_impl.cpp

    r2 r15  
    3131using namespace flair::core;
    3232
    33 IODevice_impl::IODevice_impl(const IODevice* self) {
    34     this->self=self;
    35     thread_to_wake=NULL;
    36     wake_mutex= new Mutex(self);
    37     framework_impl=::getFrameworkManagerImpl();
    38     framework=getFrameworkManager();
    39     tobelogged=false;
    40     outputtoshm=false;
     33IODevice_impl::IODevice_impl(const IODevice *self) {
     34  this->self = self;
     35  thread_to_wake = NULL;
     36  wake_mutex = new Mutex(self);
     37  framework_impl = ::getFrameworkManagerImpl();
     38  framework = getFrameworkManager();
     39  tobelogged = false;
     40  outputtoshm = false;
    4141}
    4242
     
    4444
    4545void IODevice_impl::OutputToShMem(bool enabled) {
    46     if(framework->IsLogging()) {
    47         self->Err("impossible while logging\n");
     46  if (framework->IsLogging()) {
     47    self->Err("impossible while logging\n");
     48  } else {
     49    if (LogSize() == 0) {
     50      self->Warn("log size is null, not enabling output to shared memory.");
     51      return;
     52    }
     53
     54    if (enabled) {
     55      string dev_name =
     56          getFrameworkManager()->ObjectName() + "-" + self->ObjectName();
     57      shmem = new SharedMem(self, dev_name.c_str(), LogSize());
     58      outputtoshm = true;
     59
     60      Printf("Created %s shared memory for object %s; size: %i\n",
     61             dev_name.c_str(), self->ObjectName().c_str(), LogSize());
     62      PrintLogsDescriptors();
    4863    } else {
    49         if(LogSize()==0) {
    50             self->Warn("log size is null, not enabling output to shared memory.");
    51             return;
    52         }
    53 
    54         if(enabled) {
    55             string dev_name=getFrameworkManager()->ObjectName()+ "-" + self->ObjectName();
    56             shmem=new SharedMem(self,dev_name.c_str(),LogSize());
    57             outputtoshm=true;
    58 
    59             Printf("Created %s shared memory for object %s; size: %i\n",dev_name.c_str(),self->ObjectName().c_str(),LogSize());
    60             PrintLogsDescriptors();
    61         } else {
    62             outputtoshm=false;
    63             delete shmem;
    64         }
     64      outputtoshm = false;
     65      delete shmem;
    6566    }
     67  }
    6668}
    6769
    6870void IODevice_impl::PrintLogsDescriptors(void) {
    69     //own logs
    70     for(size_t i=0;i<datasToLog.size();i++) datasToLog.at(i)->pimpl_->PrintLogDescriptor();
    71     //childs logs
    72     for(size_t i=0;i<self->TypeChilds()->size();i++) ((IODevice*)self->TypeChilds()->at(i))->pimpl_->PrintLogsDescriptors();
    73     //manually added logs
    74     for(size_t i=0;i<devicesToLog.size();i++) devicesToLog.at(i)->pimpl_->PrintLogsDescriptors();
     71  // own logs
     72  for (size_t i = 0; i < datasToLog.size(); i++)
     73    datasToLog.at(i)->pimpl_->PrintLogDescriptor();
     74  // childs logs
     75  for (size_t i = 0; i < self->TypeChilds()->size(); i++)
     76    ((IODevice *)self->TypeChilds()->at(i))->pimpl_->PrintLogsDescriptors();
     77  // manually added logs
     78  for (size_t i = 0; i < devicesToLog.size(); i++)
     79    devicesToLog.at(i)->pimpl_->PrintLogsDescriptors();
    7580}
    7681
    7782void IODevice_impl::WriteToShMem(void) {
    78     if(outputtoshm) {
    79         size_t size=LogSize();
    80 
    81         char* buf=framework_impl->GetBuffer(size);
    82         char* buf_orig=buf;
    83         if(buf==NULL) {
    84             self->Err("err GetBuffer\n");
    85             return;
    86         }
    87 
    88         AppendLog(&buf);
    89 
    90         shmem->Write(buf_orig,size);
    91         framework_impl->ReleaseBuffer(buf_orig);
     83  if (outputtoshm) {
     84    size_t size = LogSize();
     85
     86    char *buf = framework_impl->GetBuffer(size);
     87    char *buf_orig = buf;
     88    if (buf == NULL) {
     89      self->Err("err GetBuffer\n");
     90      return;
    9291    }
    93 }
    94 
    95 void IODevice_impl::AddDeviceToLog(const IODevice* device) {
    96     if(framework->IsLogging()) {
    97         self->Err("impossible while logging\n");
    98     } else {
    99         devicesToLog.push_back(device);
    100     }
     92
     93    AppendLog(&buf);
     94
     95    shmem->Write(buf_orig, size);
     96    framework_impl->ReleaseBuffer(buf_orig);
     97  }
     98}
     99
     100void IODevice_impl::AddDeviceToLog(const IODevice *device) {
     101  if (framework->IsLogging()) {
     102    self->Err("impossible while logging\n");
     103  } else {
     104    devicesToLog.push_back(device);
     105  }
    101106}
    102107
    103108bool IODevice_impl::SetToBeLogged(void) {
    104     if(!tobelogged) {
    105         tobelogged=true;
    106         return true;
    107     } else {
    108         self->Warn("already added to log\n");
    109         return false;
    110     }
    111 }
    112 
    113 void IODevice_impl::WriteLogsDescriptors(fstream& desc_file,int *index) {
    114     //own descriptor
    115     for(size_t i=0;i<datasToLog.size();i++) datasToLog.at(i)->pimpl_->WriteLogDescriptor(desc_file,index);
    116     //childs descriptors
    117     for(size_t i=0;i<self->TypeChilds()->size();i++) ((IODevice*)self->TypeChilds()->at(i))->pimpl_->WriteLogsDescriptors(desc_file,index);
    118     //manually added logs
    119     for(size_t i=0;i<devicesToLog.size();i++) devicesToLog.at(i)->pimpl_->WriteLogsDescriptors(desc_file,index);
     109  if (!tobelogged) {
     110    tobelogged = true;
     111    return true;
     112  } else {
     113    self->Warn("already added to log\n");
     114    return false;
     115  }
     116}
     117
     118void IODevice_impl::WriteLogsDescriptors(fstream &desc_file, int *index) {
     119  // own descriptor
     120  for (size_t i = 0; i < datasToLog.size(); i++)
     121    datasToLog.at(i)->pimpl_->WriteLogDescriptor(desc_file, index);
     122  // childs descriptors
     123  for (size_t i = 0; i < self->TypeChilds()->size(); i++)
     124    ((IODevice *)self->TypeChilds()->at(i))
     125        ->pimpl_->WriteLogsDescriptors(desc_file, index);
     126  // manually added logs
     127  for (size_t i = 0; i < devicesToLog.size(); i++)
     128    devicesToLog.at(i)->pimpl_->WriteLogsDescriptors(desc_file, index);
    120129}
    121130
    122131void IODevice_impl::ResumeThread(void) {
    123     wake_mutex->GetMutex();
    124     if(thread_to_wake!=NULL) {
    125         thread_to_wake->Resume();
    126         thread_to_wake=NULL;
    127     }
    128     wake_mutex->ReleaseMutex();
    129 }
    130 
    131 void IODevice_impl::AddDataToLog(const io_data* data) {
    132     if(framework->IsLogging()) {
    133         self->Err("impossible while logging\n");
    134     } else {
    135         datasToLog.push_back(data);
    136     }
     132  wake_mutex->GetMutex();
     133  if (thread_to_wake != NULL) {
     134    thread_to_wake->Resume();
     135    thread_to_wake = NULL;
     136  }
     137  wake_mutex->ReleaseMutex();
     138}
     139
     140void IODevice_impl::AddDataToLog(const io_data *data) {
     141  if (framework->IsLogging()) {
     142    self->Err("impossible while logging\n");
     143  } else {
     144    datasToLog.push_back(data);
     145  }
    137146}
    138147
    139148size_t IODevice_impl::LogSize() const {
    140     size_t value=0;
    141 
    142     for(size_t i=0;i<datasToLog.size();i++) {
    143         value+=datasToLog.at(i)->GetDataType().GetSize();
    144     }
    145 
    146     //childs logs
    147     for(size_t i=0;i<self->TypeChilds()->size();i++) {
    148         value+=((IODevice*)self->TypeChilds()->at(i))->pimpl_->LogSize();
    149     }
    150     //manually added logs
    151     for(size_t i=0;i<devicesToLog.size();i++) {
    152         value+=devicesToLog.at(i)->pimpl_->LogSize();
    153     }
    154 
    155     return value;
     149  size_t value = 0;
     150
     151  for (size_t i = 0; i < datasToLog.size(); i++) {
     152    value += datasToLog.at(i)->GetDataType().GetSize();
     153  }
     154
     155  // childs logs
     156  for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
     157    value += ((IODevice *)self->TypeChilds()->at(i))->pimpl_->LogSize();
     158  }
     159  // manually added logs
     160  for (size_t i = 0; i < devicesToLog.size(); i++) {
     161    value += devicesToLog.at(i)->pimpl_->LogSize();
     162  }
     163
     164  return value;
    156165}
    157166
    158167void IODevice_impl::WriteLog(Time time) {
    159     if(tobelogged==false) return;
    160 
    161     size_t size=LogSize();
    162 
    163     char* buf=framework_impl->GetBuffer(sizeof(FrameworkManager_impl::log_header_t)+size);
    164     char* buf_orig=buf;
    165     if(buf==NULL) {
    166         self->Err("err GetBuffer\n");
    167         return;
    168     }
    169 
    170     FrameworkManager_impl::log_header_t header;
    171     header.device=self;
    172     header.size=size;
    173     header.time=time;
    174 
    175     memcpy(buf,&header,sizeof(FrameworkManager_impl::log_header_t));
    176     buf+=sizeof(FrameworkManager_impl::log_header_t);
    177     AppendLog(&buf);
    178 
    179     framework_impl->WriteLog(buf_orig,sizeof(FrameworkManager_impl::log_header_t)+size);
    180     framework_impl->ReleaseBuffer(buf_orig);
    181 }
    182 
    183 void IODevice_impl::AppendLog(char** ptr)
    184 {
    185     //Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
    186 
    187     //copy state to buf
    188     for(size_t i=0;i<datasToLog.size();i++) {
    189         //printf("copy\n");
    190         datasToLog.at(i)->CopyDatas(*ptr);
    191         (*ptr)+=datasToLog.at(i)->GetDataType().GetSize();
    192     }
    193 
    194     //copy linked states to buf
    195     for(size_t i=0;i<self->TypeChilds()->size();i++) {
    196         ((IODevice*)self->TypeChilds()->at(i))->pimpl_->AppendLog(ptr);
    197     }
    198     //copy manually added logs to buf
    199     for(size_t i=0;i<devicesToLog.size();i++) {
    200         devicesToLog.at(i)->pimpl_->AppendLog(ptr);
    201         //devices.at(i)->DataToLog()->CopyDatas(*ptr);
    202         //(*ptr)+=devices.at(i)->DataToLog()->Size();
    203     }
    204     //Printf("AppendLog %s ok\n",self->ObjectName().c_str());
    205 }
    206 
    207 int IODevice_impl::SetToWake(const Thread* thread) {
    208     int status=0;
    209 
    210     wake_mutex->GetMutex();
    211     if(thread_to_wake==NULL) {
    212         thread_to_wake=(Thread*)thread;
    213     } else {
    214         status=-1;
    215     }
    216     wake_mutex->ReleaseMutex();
    217 
    218     return status;
    219 }
     168  if (tobelogged == false)
     169    return;
     170
     171  size_t size = LogSize();
     172
     173  char *buf = framework_impl->GetBuffer(
     174      sizeof(FrameworkManager_impl::log_header_t) + size);
     175  char *buf_orig = buf;
     176  if (buf == NULL) {
     177    self->Err("err GetBuffer\n");
     178    return;
     179  }
     180
     181  FrameworkManager_impl::log_header_t header;
     182  header.device = self;
     183  header.size = size;
     184  header.time = time;
     185
     186  memcpy(buf, &header, sizeof(FrameworkManager_impl::log_header_t));
     187  buf += sizeof(FrameworkManager_impl::log_header_t);
     188  AppendLog(&buf);
     189
     190  framework_impl->WriteLog(buf_orig,
     191                           sizeof(FrameworkManager_impl::log_header_t) + size);
     192  framework_impl->ReleaseBuffer(buf_orig);
     193}
     194
     195void IODevice_impl::AppendLog(char **ptr) {
     196  // Printf("AppendLog %s %x\n",self->ObjectName().c_str(),*ptr);
     197
     198  // copy state to buf
     199  for (size_t i = 0; i < datasToLog.size(); i++) {
     200    // printf("copy\n");
     201    datasToLog.at(i)->CopyDatas(*ptr);
     202    (*ptr) += datasToLog.at(i)->GetDataType().GetSize();
     203  }
     204
     205  // copy linked states to buf
     206  for (size_t i = 0; i < self->TypeChilds()->size(); i++) {
     207    ((IODevice *)self->TypeChilds()->at(i))->pimpl_->AppendLog(ptr);
     208  }
     209  // copy manually added logs to buf
     210  for (size_t i = 0; i < devicesToLog.size(); i++) {
     211    devicesToLog.at(i)->pimpl_->AppendLog(ptr);
     212    // devices.at(i)->DataToLog()->CopyDatas(*ptr);
     213    //(*ptr)+=devices.at(i)->DataToLog()->Size();
     214  }
     215  // Printf("AppendLog %s ok\n",self->ObjectName().c_str());
     216}
     217
     218int IODevice_impl::SetToWake(const Thread *thread) {
     219  int status = 0;
     220
     221  wake_mutex->GetMutex();
     222  if (thread_to_wake == NULL) {
     223    thread_to_wake = (Thread *)thread;
     224  } else {
     225    status = -1;
     226  }
     227  wake_mutex->ReleaseMutex();
     228
     229  return status;
     230}
Note: See TracChangeset for help on using the changeset viewer.