C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
logger.c
Go to the documentation of this file.
1 
6 #include "common.h"
7 #include "logger.h"
8 
9 //Array to maintain current log levels to be used
10 static bool log_level[MAX_LOG_LEVELS] = {false};
11 static bool module_logs[MAX_MODULES] = {false};
12 
18 {
19  int i;
20 
21  for (i = 0; i < MAX_LOG_LEVELS; i++) {
22  log_level[i] = true;
23  }
24 
25  printf("Logger init\n");
26 
27 }
28 
33 int app_log(char *module, int level, char *prefix, const char *format, ...)
34 {
35  int len = 0;
36  va_list args;
37 
38  va_start(args, format);
39 
40  if (level < MAX_LOG_LEVELS && log_level[level] == true) {
41  char time_buff[64];
42  int millisec;
43  struct timeval tv;
44  time_t now = time(0);
45 
46  gettimeofday(&tv, NULL);
47  millisec = (tv.tv_usec/1000);
48  strftime(time_buff, 64, "%H:%M:%S", localtime(&now));
49  printf("%s.%03d ", time_buff, millisec);
50  switch (level) {
51  case __LOG_ERROR__:
52  printf("[ERR");
53  break;
54  case __LOG_WARN__:
55  printf("[WRN");
56  break;
57  case __LOG_INFO__:
58  printf("[INF");
59  break;
60  case __LOG_DEBUG__:
61  printf("[DBG");
62  break;
63  case __LOG_TRACE__:
64  printf("[TRC");
65  break;
66  default:
67  printf("[UDF");
68  break;
69  }
70 
71  printf(":%s] ",module);
72 
73  if (prefix != NULL) {
74  printf("{%s} ", prefix);
75  }
76  len = vprintf(format, args);
77  }
78 
79  va_end(args);
80 
81  return len;
82 }
Top level include containg common headers.
int app_log(char *module, int level, char *prefix, const char *format,...)
Used for defining different logging modules such as warn, error, debug, trace ...
Definition: logger.c:33
void logger_init()
Initailize logger module
Definition: logger.c:17
Defines Macros for logging.
#define __LOG_TRACE__
log level Trace
Definition: logger.h:11
#define MAX_MODULES
Definition: logger.h:14
#define __LOG_ERROR__
log level Error
Definition: logger.h:7
#define __LOG_INFO__
log level Info
Definition: logger.h:9
#define __LOG_WARN__
log level Warn
Definition: logger.h:8
#define MAX_LOG_LEVELS
Definition: logger.h:13
#define __LOG_DEBUG__
log level Debug
Definition: logger.h:10