Browse Source

Bugfix on logger with ident field etc

Yann Weber 4 years ago
parent
commit
d2e7517348
3 changed files with 36 additions and 12 deletions
  1. 14
    9
      include/logger.h
  2. 21
    2
      src/logger.c
  3. 1
    1
      tests/check_logger.c

+ 14
- 9
include/logger.h View File

@@ -63,8 +63,8 @@
63 63
 #define PYFCGI_LOG_DTM_LEN 25
64 64
 #define PYFCGI_LOG_LVL_LEN 7
65 65
 #define PYFCGI_LOG_TYP_LEN 7
66
-#define PYFCGI_LOG_PID_LEN 4
67
-#define PYFCGI_LOG_PID_FMT "%04d"
66
+#define PYFCGI_LOG_PID_LEN 6
67
+#define PYFCGI_LOG_PID_FMT "%6d"
68 68
 
69 69
 #define SYSLOG_syslog syslog
70 70
 #define SYSLOG_vsyslog vsyslog
@@ -98,7 +98,7 @@
98 98
 #define LOG_DEBUG (8 << 4)
99 99
 
100 100
 /**@brief Convert a PyFCGI loglevel in a syslog one */
101
-#define PYFCGI_SYSLOG_LVL(lvl) (SYSLOG_LVLS[(lvl & 0xF0) >> 4])
101
+#define PYFCGI_SYSLOG_LVL(lvl) (((lvl & 0xF0) >> 4)-1)
102 102
 
103 103
 /**@brief Macro checking if syslog is activated and log a message
104 104
  * @params int lvl the PyFCGI log level
@@ -125,13 +125,15 @@
125 125
 /**@ingroup cong_logger_flags */
126 126
 #define PYFCGI_LOG_FSYSLOG 1
127 127
 /**@brief Indicate if the logger should try to reopen on failure
128
+ * @warn not implemented
128 129
  * @ingroup cong_logger_flags */
129 130
 #define PYFCGI_LOG_FRETRY 2
130 131
 /**@brief Exit if failure
132
+ * @warn not implemented
131 133
  * @ingroup cong_logger_flags */
132 134
 #define PYFCGI_LOG_FEXIT_ONFAIL 4
133 135
 
134
-#define PYFCGI_LOGGER_FMT_DEFAULT "{datetime} {ident} {level}({pid}) {msg}"
136
+#define PYFCGI_LOGGER_FMT_DEFAULT "{datetime} {ident}[{pid}] {level} {msg}"
135 137
 #define PYFCGI_LOGGER_TIME_FMT_DEFAULT "%F %T%z"
136 138
 
137 139
 
@@ -151,9 +153,7 @@ typedef struct ident_args_s ident_args_t;
151 153
 typedef union pyfcgi_logger_field_args pyfcgi_logger_field_args_u;
152 154
 
153 155
 
154
-static const short SYSLOG_LVLS[8] = {LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
155
-                                     LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
156
-static const char* PYFCGI_LOGGER_LVLNAME[] = {
156
+static const char* PYFCGI_LOGGER_LVLNAME[] __attribute__((unused)) = {
157 157
 	" Emerge",
158 158
 	"  Alert",
159 159
 	" Critic",
@@ -163,8 +163,8 @@ static const char* PYFCGI_LOGGER_LVLNAME[] = {
163 163
 	"   Info",
164 164
 	"  Debug"};
165 165
 
166
-static const char* PYFCGI_LOGGER_TYPNAME[] = {"Global", "Access", "Internal",
167
-                                              "Worker", "Master"};
166
+static const char* PYFCGI_LOGGER_TYPNAME[] __attribute__((unused)) =
167
+	{"Global", "Access", "Internal", "Worker", "Master"} ;
168 168
 
169 169
 
170 170
 /**@defgroup conf_logger_format Logline format
@@ -321,6 +321,11 @@ int pyfcgi_logger_init();
321 321
  */
322 322
 int pyfcgi_logger_stop();
323 323
 
324
+/**@brief Enable syslog logging with given ident
325
+ * @param char* ident if NULL use current ident
326
+ */
327
+void pyfcgi_logger_enable_syslog(char*);
328
+
324 329
 /**@brief Stop & free an individual logger */
325 330
 void _pyfcgi_logger_free(pyfcgi_logger_t*);
326 331
 

+ 21
- 2
src/logger.c View File

@@ -32,6 +32,22 @@ int pyfcgi_logger_stop()
32 32
 	return 0;
33 33
 }
34 34
 
35
+void pyfcgi_logger_enable_syslog(char* nident)
36
+{
37
+	pyfcgi_conf_logger_t *conf;
38
+	conf = &PyFCGI_conf.logs;
39
+	if(conf->flags & PYFCGI_LOG_FSYSLOG)
40
+	{
41
+		closelog();
42
+	}
43
+	conf->flags |= PYFCGI_LOG_FSYSLOG;
44
+	if(!nident)
45
+	{
46
+		nident = conf->ident;
47
+	}
48
+	openlog(nident, LOG_CONS | LOG_PERROR, LOG_DAEMON | LOG_USER);
49
+}
50
+
35 51
 void _pyfcgi_logger_free(pyfcgi_logger_t *logger)
36 52
 {
37 53
 	free(logger->filename);
@@ -264,10 +280,13 @@ int pyfcgi_logger_format_bufinit(pyfcgi_logger_format_t* fmt)
264 280
 			{
265 281
 				
266 282
 				case pyfcgi_logger_field_const:
267
-				case pyfcgi_logger_field_ident:
268 283
 					memcpy(cur, fmt->fields[i].val,
269 284
 						fmt->fields[i].len);
270 285
 					break;
286
+				case pyfcgi_logger_field_ident:
287
+					memcpy(cur, *((char**)fmt->fields[i].val),
288
+						fmt->fields[i].len);
289
+					break;
271 290
 				case pyfcgi_logger_field_pid:
272 291
 					snprintf(pid, PYFCGI_LOG_PID_LEN+1,
273 292
 						PYFCGI_LOG_PID_FMT,
@@ -540,7 +559,7 @@ int pyfcgi_logger_set_ident(const char* new_ident)
540 559
 			if(field->type == pyfcgi_logger_field_ident)
541 560
 			{
542 561
 				field->len = len;
543
-				upd = 0;
562
+				upd = 1;
544 563
 			}
545 564
 		}
546 565
 		if(upd)

+ 1
- 1
tests/check_logger.c View File

@@ -59,7 +59,7 @@ START_TEST(test_logger_add)
59 59
 {
60 60
 	char tmplog[128];
61 61
 	char logfmt[] = "{level} {ident} {pid} {msg}";
62
-	char expected[] = "  Alert  0000 Hello world ! foobar 04\n";
62
+	char expected[] = "  Alert       0 Hello world ! foobar 04\n";
63 63
 	int ret;
64 64
 	pyfcgi_conf_logger_t *conf;
65 65
 	pyfcgi_logger_t *logger;

Loading…
Cancel
Save