log.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <errno.h>
  6. #include "log.h"
  7. #define PLOT_LINES 6
  8. static void log_add_ent(log_t *l, logent_t *le);
  9. static void logent_destroy(logent_t *le);
  10. static void fatal_error(const char *format, ...);
  11. static f_t *finit(char *name, const char *mode);
  12. static void fend(f_t *f);
  13. /**
  14. * Get the time: function to use as logfunc_t
  15. **/
  16. void gettime(unsigned long long *t) {
  17. struct timeval tp;
  18. unsigned long long ctime;
  19. gettimeofday( &tp, NULL );
  20. ctime = tp.tv_sec;
  21. ctime *= 1000000;
  22. ctime += tp.tv_usec;
  23. *t = ctime;
  24. }
  25. log_t *log_init(char *fn, const char *mode, char *xlabel, char *ylabel, logfunc_t func) {
  26. char buf[256];
  27. stat_t st;
  28. log_t *l = (log_t *) malloc(sizeof(log_t));
  29. if (l == NULL)
  30. fatal_error("Could not allocate memory for the new log [%s].", fn);
  31. if (stat(LOG_DIR, &st) != 0)
  32. if (mkdir(LOG_DIR, 0777))
  33. fatal_error("Could not create directory: %s", LOG_DIR);
  34. sprintf(buf, "%s/%s-%s", LOG_DIR, fn, LOG_PLT);
  35. l->file = finit(buf, mode);
  36. if (l->file->lines == 0)
  37. l->file->lines = PLOT_LINES;
  38. l->entries = 0;
  39. l->entry = NULL;
  40. l->xlabel = strdup(xlabel);
  41. l->ylabel = strdup(ylabel);
  42. l->func = func;
  43. return l;
  44. }
  45. logent_t *logent_init(log_t *l, char *desc, ...) {
  46. char buf[256];
  47. char *id;
  48. va_list vargs;
  49. logent_t *le = (logent_t *)malloc(sizeof(logent_t));
  50. if (l == NULL)
  51. fatal_error("Log is NULL\n");
  52. if (le == NULL)
  53. fatal_error("Could not allocate memory for log entry: %s:%s\n", l->file->fn, desc);
  54. le->desc = strdup(desc);
  55. va_start(vargs, desc);
  56. id = va_arg(vargs, char*);
  57. va_end(vargs);
  58. if (id != NULL)
  59. sprintf(buf, "%s-%d.data", l->file->fn, l->file->lines - PLOT_LINES + l->entries);
  60. else
  61. sprintf(buf, "%s-%s.data", l->file->fn, id);
  62. printf("FN: %s\n", buf);
  63. le->file = finit(buf, l->file->mode);
  64. log_add_ent(l, le);
  65. return le;
  66. }
  67. void logent_start(logent_t *le, ...) {
  68. va_list vargs;
  69. if (le == NULL)
  70. fatal_error("Log entry is NULL\n");
  71. va_start(vargs, le);
  72. le->log->func(&le->t1);
  73. va_end(vargs);
  74. }
  75. void logent_end(logent_t *le, int idx, int ent, ...) {
  76. int res;
  77. va_list vargs;
  78. if (le == NULL)
  79. fatal_error("Logentry is NULL\n");
  80. va_start(vargs, ent);
  81. le->log->func(&le->t2, vargs);
  82. va_end(vargs);
  83. if (ent <= 0)
  84. ent = 1;
  85. res = (int)(le->t2 - le->t1) / ent;
  86. fprintf(le->file->f, "%d %d\n", idx, res);
  87. fflush(le->file->f);
  88. }
  89. void log_destroy(log_t *l) {
  90. int i;
  91. if (ftell(l->file->f) == 0) {
  92. fprintf(l->file->f, "set terminal postscript color\n");
  93. fprintf(l->file->f, "set output \"%s-%s\"\n", l->file->fn, LOG_CMP);
  94. fprintf(l->file->f, "set autoscale\n");
  95. fprintf(l->file->f, "set xlabel \"%s\"\nset ylabel \"%s\"\n", l->xlabel, l->ylabel);
  96. fprintf(l->file->f, "set key right\nplot");
  97. }
  98. for (i = 0; i < l->entries; i++) {
  99. if (l->entry[i]->file->lines == 0) {
  100. fprintf(l->file->f, " \\\n\"%s\" w linespoints lw 2 t \"%s\",",
  101. l->entry[i]->file->fn, l->entry[i]->desc
  102. );
  103. }
  104. logent_destroy(l->entry[i]);
  105. }
  106. fflush(l->file->f);
  107. fend(l->file);
  108. free(l->xlabel);
  109. free(l->ylabel);
  110. if (l->entry != NULL)
  111. free(l->entry);
  112. }
  113. /**
  114. * When something bad happens.... we end!
  115. **/
  116. static void fatal_error(const char *format, ...) {
  117. va_list vargs;
  118. fprintf(stdout, "[FATAL] ");
  119. va_start(vargs, format);
  120. vfprintf(stdout, format, vargs);
  121. va_end(vargs);
  122. exit(EXIT_FAILURE);
  123. }
  124. static f_t *finit(char *name, const char *mode) {
  125. char c;
  126. f_t *f = (f_t *)malloc(sizeof(f_t));
  127. if (f == NULL)
  128. fatal_error("Could not allocate memory for %s\n", name);
  129. f->fn = strdup(name);
  130. f->f = fopen(f->fn, mode);
  131. f->lines = 0;
  132. f->mode = mode;
  133. if (f->f == NULL)
  134. fatal_error("Coult not open %s[%s]\n", name, mode);
  135. while ((c = fgetc(f->f)) != EOF)
  136. if (c == '\n')
  137. f->lines++;
  138. fseek(f->f, 0, SEEK_END);
  139. return f;
  140. }
  141. static void fend(f_t *f) {
  142. if (f == NULL)
  143. fatal_error("Cant close NULL-file\n");
  144. if (f->f == NULL)
  145. return;
  146. printf("[FILE] stored in: %s\n", f->fn);
  147. free(f->fn);
  148. fclose(f->f);
  149. }
  150. static void log_add_ent(log_t *l, logent_t *le) {
  151. int i;
  152. logent_t **entry = (logent_t **)malloc(sizeof(logent_t*) * (l->entries + 1));
  153. for (i = 0; i < l->entries; i++)
  154. entry[i] = l->entry[i];
  155. entry[l->entries++] = le;
  156. if (l->entry != NULL)
  157. free(l->entry);
  158. l->entry = entry;
  159. le->log = l;
  160. }
  161. static void logent_destroy(logent_t *le) {
  162. if (le == NULL)
  163. fatal_error("Cant close log entry 0x%p\n", le);
  164. if (le->file->f != NULL)
  165. fend(le->file);
  166. le->file->f = NULL;
  167. free(le->desc);
  168. free(le);
  169. }