Guest User

latest async logger

a guest
Aug 24th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | Source Code | 0 0
  1. #include <aio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include <pthread.h>
  10.  
  11. #include "log.h"
  12. #include "stdext.h"
  13.  
  14. static const char *logprefix[4] = {
  15.     "ALL: ", "INFO: ", "WARINING: ", "ERROR: "
  16. };
  17.  
  18. pthread_mutex_t logmutex;
  19.  
  20. static struct aiocb defAIO = {0};
  21. static struct aiocb *logAio(struct aiocb *aio);
  22. static void logtofile(const char *msg);
  23. // appends runtime info to dest
  24. static void appendRTimeInfo(char *dest, const char *file,
  25. #ifndef NO_FUNC
  26.         const char *func,
  27. #endif
  28.         int line);
  29.  
  30. static enum LogLevel stdoutMask, fileMask;
  31. static FILE *logfile;
  32.  
  33. void loginit(enum LogLevel _stdoutMask, enum LogLevel _fileMask) {
  34.     stdoutMask = _stdoutMask;
  35.     fileMask = _fileMask;
  36.     logfile = fopen(LOGFILE_NAME, "a");
  37.     if(!logfile) {
  38.         fprintf(stderr, "ERROR: Couldn't open logfile, %s, %s\n", LOGFILE_NAME, strerror(errno));
  39.     }
  40.     pthread_mutex_init(&logmutex, 0);
  41. }
  42.  
  43. void logdestroy() {
  44.     printf("destr: errn: %s\n", strerror(errno));
  45.     return;
  46.     if(pthread_mutex_trylock(&logmutex) == -1){// && errno == EBUSY) {
  47.         puts("here");
  48.         while(aio_error(logAio(NULL)) == EINPROGRESS);
  49.         free(logAio(NULL));
  50.         logAio(NULL+1);
  51.         goto cl;
  52.     }
  53.     printf("destr: errn: %s\n", strerror(errno));
  54.     struct aiocb *aio = logAio(NULL);
  55.     if(aio) {
  56.         puts("Here");
  57.         //unsigned depth;
  58.         //while(aio_error(aio) == EINPROGRESS && depth < MAX_AIO_WAIT)
  59.         //  depth++;
  60.         const struct aiocb *aios[1];
  61.         aios[0] = aio;
  62.         if(aio_suspend(aios, 1, NULL) == 0)
  63.             free(aio);
  64.         //if(depth != 500)
  65.         //  free(aio);
  66.     }
  67. cl:
  68.     if(logfile) {
  69.         fclose(logfile);
  70.         logfile = NULL;
  71.     }
  72.     pthread_mutex_unlock(&logmutex);
  73.     pthread_mutex_destroy(&logmutex);
  74. }
  75.  
  76. void __logf(enum LogLevel level, const char *file,
  77. #ifndef NO_FUNC
  78.         const char *func,
  79. #endif
  80.         int line, const char *format, ...) {
  81.     if(level == L_NONE)
  82.         return;
  83.     va_list ap;
  84.     va_start(ap, format);
  85.  
  86.     char message[512];
  87.     strncpy(message, logprefix[level], 9);
  88.     {
  89.         char tmp[256];
  90.         vsnprintf(tmp, 256, format, ap);
  91.         strcat(message, tmp);
  92.     }
  93.     va_end(ap);
  94.     appendRTimeInfo(message, file,
  95. #ifndef NO_FUNC
  96.             func,
  97. #endif
  98.             line);
  99.     if(stdoutMask != L_NONE && level >= stdoutMask)
  100.         printf("%s\n", message);
  101.     if(fileMask != L_NONE && level >= fileMask)
  102.         logtofile(message);
  103. }
  104.  
  105. void __log(enum LogLevel level, const char *file,
  106. #ifndef NO_FUNC
  107.         const char *func,
  108. #endif
  109.         int line, const char *msg) {
  110.     if(level == L_NONE)
  111.         return;
  112.     char message[512];
  113.     strncpy(message, logprefix[level], 9);
  114.     strncat(message, msg, 256);
  115.  
  116.     appendRTimeInfo(message, file,
  117. #ifndef NO_FUNC
  118.             func,
  119. #endif
  120.             line);
  121.  
  122.     // outputing
  123.     if(stdoutMask != L_NONE && level >= stdoutMask)
  124.         printf("%s\n", message);
  125.     if(fileMask != L_NONE && level >= fileMask)
  126.         logtofile(message);
  127. }
  128.  
  129. static void appendRTimeInfo(char *dest, const char *file,
  130. #ifndef NO_FUNC
  131.         const char *func,
  132. #endif
  133.         int line) {
  134.     // not ASSERT because it would call log
  135. #ifdef _NTR_ASSERT_H_
  136.     __assert_m(dest != NULL, "Dest was NULL, while appending runtime info", __FILE__,
  137. # ifndef NO_FUNC
  138.             __func__,
  139. # endif
  140.             __LINE__, false);
  141. #endif
  142.  
  143.     // ignore warning because, it thinks that message can be 512 long, but it at most can be 265
  144.     snprintf(dest+strnlen(dest, 265), 512, "\n\t in file: '%s', "
  145. #ifndef NO_FUNC
  146.             "in function: %s, "
  147. #endif
  148.  
  149.             "at line: %d, errno: %s\n",
  150.             file,
  151. #ifndef NO_FUNC
  152.             func,
  153. #endif
  154.             line, strerror(errno));
  155. }
  156.  
  157. static struct aiocb *logAio(struct aiocb *aio) {
  158.     static struct aiocb *val;
  159.     printf("logaio: %p %p\n", aio, val);
  160.     if(aio == NULL)
  161.         return val;
  162.     if(val != NULL)
  163.         free(val);
  164.     val = aio;
  165.     return NULL;
  166. }
  167.  
  168. static void logtofile(const char *msg) {
  169.     pthread_mutex_lock(&logmutex);
  170.     if(!logfile)
  171.         return;
  172.     // Error already printed in loginit
  173.     printf("msg:%s\n",msg);
  174.     logAio(async_write_str(fileno(logfile), (char *)msg, strnlen(msg, 512)));
  175.     pthread_mutex_unlock(&logmutex);
  176.     puts("tffinn");
  177. }
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment