// // g++ logger.cc -o logegr.exe -lpthread // #include "logger.hh" //--------------------------------------------------------------------------------------- // Global var //--------------------------------------------------------------------------------------- //LogBuffer *rbuf; const int BUFSIZE=8192; char Buf[BUFSIZE]; //--------------------------------------------------------------------------------------- // threads //--------------------------------------------------------------------------------------- pthread_t wr_thread, rd_thread; static void *write_thread (void* arg) { char line[80]; LogBuffer *rbuf=(LogBuffer *)arg; int ii=0; //FILE *fw = fopen("write.txt", "w"); while (true) { ii++; sprintf(line,"line %d channel %d \n",ii,random()%256); sleep(1); if (rbuf->write(line,strlen(line))) { printf("WRITE buffer error: ii=%d, fill=%d\n",ii,rbuf->fill_level); usleep(100000); } else printf("WRITE buffer: ii=%d fill=%d\n",ii,rbuf->level()); //else fprintf(fw,"%d %d\n",evt.time,evt.channel,rbuf->fill_level); //fflush(stdout); } return 0; } static void *read_thread (void* arg) { const int MAXBUF=8192; char text[MAXBUF+1]; LogBuffer *rbuf=(LogBuffer *)arg; int tm,ch,ii=0; //FILE *fr = fopen("read.txt", "w"); while (true) { ii++; int rsize=rbuf->read(text,100); text[rsize]=0; sleep(2); if (rsize==0) { printf("READ buffer error: ii=%d , fill=%d\n",ii,rbuf->fill_level); } else printf("READ buffer: ii=%d size=%d fill=%d log=%s \n",ii, rsize, rbuf->level(), text ); //else fprintf(fr,"%d %d\n",evt.time,evt.channel); //fflush(stdout); } return 0; } //--------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { int tm, ch; LogBuffer *rbuf = new LogBuffer(Buf,BUFSIZE); printf("Start threads \n"); int ret_wr = pthread_create (&wr_thread, 0, write_thread,rbuf); if (ret_wr) { perror ("pthread_create"); } int ret_rd = pthread_create (&rd_thread, 0, read_thread,rbuf); if (ret_rd) { perror ("pthread_create"); } printf("join threads \n"); void *retval_wr, *retval_rd; if (pthread_join (wr_thread, &retval_wr)) perror ("pthread_join"); if (pthread_join (rd_thread, &retval_rd)) perror ("pthread_join"); }