KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
log.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : log.h
11  * Created : 12 sept. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <stdbool.h>
18 #include <string.h>
19 
20 #include "util/log.h"
21 
22 #define LOG_BUF_SIZE 128
23 
24 static const char * _logLevel2Name[6] =
25 {
26  "TRCE", "DBUG", "INFO", "WARN", "ERRO", "FATL"
27 };
28 
29 static bool _logEnabled = true;
30 static LogLevel _logLevel = LOG_LEVEL_DEFAULT;
31 
32 #define TIME_CHARS 10
33 #define TIME_CHARS_S "10"
34 
35 
36 static char _prevBuf[LOG_BUF_SIZE - TIME_CHARS];
37 static bool _repeat = false;
38 
39 
40 static void _logWriteIntr(LogModInfo * mod, LogLevel level, const char * msg)
41 {
42  if (!_logEnabled) return;
43  if (level < _logLevel) return;
44  if (level < mod->level) return;
45 
46  char logbuf[LOG_BUF_SIZE];
47  int l;
48  l = sprintf(logbuf, "%" TIME_CHARS_S "u [%s] %s - %s", _logTime(), _logLevel2Name[level], msg, mod->name);
49  logbuf[l] = '\0';
50 
51  if (strncmp(logbuf + TIME_CHARS, _prevBuf, LOG_BUF_SIZE - TIME_CHARS) == 0) {
52  if (!_repeat) {
53  _logOut(level, "... Last line repeats, suppressed");
54  _repeat = true;
55  }
56  return;
57  }
58  _repeat = false;
59  memcpy(_prevBuf, logbuf + TIME_CHARS, LOG_BUF_SIZE - TIME_CHARS);
60 
61  _logOut(level, logbuf);
62 
63 
64 }
65 
66 void _logWrite(LogModInfo * mod, LogLevel level, const char * fmt, ...)
67 {
68  va_list va;
69  va_start(va, fmt);
70  char buf[128];
71  int l;
72  l = vsprintf(buf, fmt, va);
73  buf[l] = '\0';
74  va_end(va);
75  _logWriteIntr(mod, level, buf);
76 }
77 
78 
79 void logEnable(bool enable)
80 {
81 
82  _logEnabled = enable;
83 }
84 
86 {
87  _logLevel = level;
88 }
89 
uint32_t _logTime()
Stub function, which should return the current time in seconds, since whatever.
Definition: sys.c:201
void logEnable(bool enable)
Enable logging.
Definition: log.c:79
LogLevel
Logging levels.
Definition: log.h:68
void _logWrite(LogModInfo *mod, LogLevel level, const char *msg,...)
Write a logging statement.
Definition: log.c:66
#define LOG_LEVEL_DEFAULT
The default logging level.
Definition: log.h:101
Module info structure.
Definition: log.h:86
void logSetGlobalLevel(LogLevel level)
Set the global log level.
Definition: log.c:85
void _logOut(LogLevel level, char *logLine)
External dependency function.
Definition: sys.c:353
Implements a generic logger facility.