annotate logging.c @ 248:eaac96094640

Minor style improvements
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 04 May 2015 17:46:52 +0200
parents ec81395bf08d
children 307fd62f376d
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>
229
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
4 #include <ansi_c.h>
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
5
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
6 #include "config.h"
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
7 #include "logging.h"
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
8
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
9 /* 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
10 extern double utc;
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
11 /* main panel id */
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
12 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
13
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
14
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
15 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
16 {
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
17 int rv, panel;
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
18 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
19
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
20 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
21 l->fd = -1;
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
22
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
23 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
24 if (panel < 0)
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
25 return -1;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
26 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
27
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
28 /* configuration file path */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
29 GetIniFilePath(path);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
30
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
31 /* load configuration file */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
32 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
33 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
34
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
35 /* logging file name */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
36 rv = Ini_GetStringCopy(configuration, "logging", "filename", &filename);
229
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
37 if (rv > 0) {
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
38 __logmessage(l, INFO, "logging to '%s'", filename);
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
39 l->fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744);
229
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
40 if (l->fd < 0)
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
41 __logmessage(l, ERROR, "open log file %s: %s", filename, strerror(errno));
28a56e4c06a4 Improvements to log file handling
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 199
diff changeset
42 }
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
43
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
44 free(filename);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
45 Ini_Dispose(configuration);
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
46
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
47 return 0;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
48 }
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
49
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
50
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
51 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
52 {
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
53 static const char *levels[] = {
248
eaac96094640 Minor style improvements
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
54 [ERROR] = "ERROR",
eaac96094640 Minor style improvements
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
55 [DEBUG] = "DEBUG",
eaac96094640 Minor style improvements
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
56 [INFO] = "INFO",
eaac96094640 Minor style improvements
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 239
diff changeset
57 [WARNING] = "WARNING",
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
58 };
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
59
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
60 char msg[1024];
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
61 int len = 0;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
62
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
63 /* timestamp */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
64 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
65 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
66 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
67 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
68
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
69 /* level */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
70 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
71
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
72 /* message */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
73 va_list args;
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
74 va_start(args, frmt);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
75 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
76 va_end(args);
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
77
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
78 /* newline */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
79 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
80 msg[len++] = '\r';
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
81 msg[len++] = '\n';
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
82
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
83 /* string terminator */
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
84 msg[len] = '\0';
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
85
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
86 /* display message */
169
97112b45b838 Get Sr data logger parameters from configuration file. Code reorganization.
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents:
diff changeset
87 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
88
199
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
89 /* report error */
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
90 if (level == ERROR)
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
91 l->onerror(level, msg);
111610d871af Show error state in main user inteface panel
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 191
diff changeset
92
191
0fed60877099 Allow to write log to file specified in configuration file
Daniele Nicolodi <daniele.nicolodi@obspm.fr>
parents: 169
diff changeset
93 /* 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
94 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
95 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
96 }