annotate logging.c @ 215:e925664b4b1b

Add streq() and strneq() definitions to utils.h
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 14 Apr 2014 12:46:14 +0200
parents 111610d871af
children 28a56e4c06a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
1 #include <ansi_c.h>
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
2 #include <inifile.h>
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
3 #include <lowlvlio.h>
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
4
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
5 #include "config.h"
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
6 #include "logging.h"
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8 /* this refers to the event time defined as a global variable in the main program */
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 extern double utc;
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
10 /* main panel id */
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
11 extern unsigned int MainPanel;
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
12
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
13
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
14 int __logger_init(struct logger *l, void(*onerror)(int, const char *))
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
15 {
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
16 int rv, panel;
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
17 char path[MAX_PATHNAME_LEN], *filename;
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
18
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
19 l->onerror = onerror;
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
20 l->fd = -1;
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
21
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
22 panel = LoadPanel(0, "FXAnalyse.uir", LOGGING);
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
23 if (panel < 0)
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
24 return -1;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 l->panel = panel;
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
26
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
27 /* configuration file path */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
28 GetIniFilePath(path);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
29
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
30 /* load configuration file */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
31 IniText configuration = Ini_New(TRUE);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
32 Ini_ReadFromFile(configuration, path);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
33
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
34 /* logging file name */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
35 rv = Ini_GetStringCopy(configuration, "logging", "filename", &filename);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
36 if (rv > 0)
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
37 l->fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
38
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
39 free(filename);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
40 Ini_Dispose(configuration);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
41
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
42 return 0;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
43 }
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
44
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
45
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
46 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...)
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 {
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
48 static const char *levels[] = {
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49 "DEBUG",
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50 "INFO",
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 "WARNING",
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
52 "ERROR",
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
53 };
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
54
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
55 char msg[1024];
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
56 int len = 0;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
57
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
58 /* timestamp */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59 len += sprintf(msg, "%014.3f ", utc);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 time_t now = time(NULL);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 struct tm *t = localtime(&now);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62 len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
63
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
64 /* level */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
65 len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
66
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
67 /* message */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
68 va_list args;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
69 va_start(args, frmt);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
71 va_end(args);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
72
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
73 /* newline */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
74 len = MIN(len, sizeof(msg) - 3);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
75 msg[len++] = '\r';
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
76 msg[len++] = '\n';
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
77
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
78 /* string terminator */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
79 msg[len] = '\0';
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
80
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
81 /* display message */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
82 SetCtrlVal(l->panel, LOGGING_LOGGING, msg);
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
83
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
84 /* report error */
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
85 if (level == ERROR)
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
86 l->onerror(level, msg);
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
87
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
88 /* write to log file */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
89 if (l->fd >= 0)
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
90 write(l->fd, msg, len);
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
91 }