Browse Source

[console] Add "log message" console usage and an internal syslog() call

Provide an internal syslog() function (unrelated to the syslog
console) which can be used to create log messages with specified
priorities.

The build-time constant LOG_LEVEL can be used to select the minimum
required priority for log messages.  Any messages that do not have a
sufficient priority will be ignored (and will be optimised away at
compile-time).

The default LOG_LEVEL is LOG_NONE.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
24b7296319
5 changed files with 156 additions and 3 deletions
  1. 2
    0
      src/config/console.h
  2. 62
    0
      src/core/log.c
  3. 5
    2
      src/include/ipxe/console.h
  4. 3
    1
      src/include/ipxe/syslog.h
  5. 84
    0
      src/include/syslog.h

+ 2
- 0
src/config/console.h View File

@@ -23,6 +23,8 @@ FILE_LICENCE ( GPL2_OR_LATER );
23 23
 
24 24
 #define	KEYBOARD_MAP	us
25 25
 
26
+#define	LOG_LEVEL	LOG_NONE
27
+
26 28
 #include <config/local/console.h>
27 29
 
28 30
 #endif /* CONFIG_CONSOLE_H */

+ 62
- 0
src/core/log.c View File

@@ -0,0 +1,62 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * System logger
24
+ *
25
+ */
26
+
27
+#include <stdarg.h>
28
+#include <syslog.h>
29
+#include <ipxe/console.h>
30
+
31
+/**
32
+ * Write message to system log
33
+ *
34
+ * @v fmt		Format string
35
+ * @v args		Arguments
36
+ */
37
+void log_vprintf ( const char *fmt, va_list args ) {
38
+	int saved_usage;
39
+
40
+	/* Mark console as in use for log messages */
41
+	saved_usage = console_set_usage ( CONSOLE_USAGE_LOG );
42
+
43
+	/* Print message */
44
+	vprintf ( fmt, args );
45
+
46
+	/* Restore console usage */
47
+	console_set_usage ( saved_usage );
48
+}
49
+
50
+/**
51
+ * Write message to system log
52
+ *
53
+ * @v fmt		Format string
54
+ * @v ...		Arguments
55
+ */
56
+void log_printf ( const char *fmt, ... ) {
57
+	va_list args;
58
+
59
+	va_start ( args, fmt );
60
+	log_vprintf ( fmt, args );
61
+	va_end ( args );
62
+}

+ 5
- 2
src/include/ipxe/console.h View File

@@ -120,9 +120,12 @@ struct console_driver {
120 120
 /** Text-based user interface */
121 121
 #define CONSOLE_USAGE_TUI 0x0004
122 122
 
123
+/** Log messages */
124
+#define CONSOLE_USAGE_LOG 0x0008
125
+
123 126
 /** All console usages */
124
-#define CONSOLE_USAGE_ALL \
125
-	( CONSOLE_USAGE_STDOUT | CONSOLE_USAGE_DEBUG | CONSOLE_USAGE_TUI )
127
+#define CONSOLE_USAGE_ALL ( CONSOLE_USAGE_STDOUT | CONSOLE_USAGE_DEBUG | \
128
+			    CONSOLE_USAGE_TUI | CONSOLE_USAGE_LOG )
126 129
 
127 130
 /** @} */
128 131
 

+ 3
- 1
src/include/ipxe/syslog.h View File

@@ -9,6 +9,8 @@
9 9
 
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12
+#include <syslog.h>
13
+
12 14
 /** Syslog server port */
13 15
 #define SYSLOG_PORT 514
14 16
 
@@ -28,7 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
28 30
  *
29 31
  * This is a policy decision
30 32
  */
31
-#define SYSLOG_SEVERITY 6 /* informational */
33
+#define SYSLOG_SEVERITY LOG_INFO
32 34
 
33 35
 /** Syslog priority */
34 36
 #define SYSLOG_PRIORITY( facility, severity ) ( 8 * (facility) + (severity) )

+ 84
- 0
src/include/syslog.h View File

@@ -0,0 +1,84 @@
1
+#ifndef _SYSLOG_H
2
+#define _SYSLOG_H
3
+
4
+/** @file
5
+ *
6
+ * System logger
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#include <stdarg.h>
13
+#include <config/console.h>
14
+
15
+/**
16
+ * @defgroup syslogpri Syslog priorities
17
+ *
18
+ * These values are chosen to match those used in the syslog network
19
+ * protocol (RFC 5424).
20
+ *
21
+ * @{
22
+ */
23
+
24
+/** Emergency: system is unusable */
25
+#define LOG_EMERG 0
26
+
27
+/** Alert: action must be taken immediately */
28
+#define LOG_ALERT 1
29
+
30
+/** Critical: critical conditions */
31
+#define LOG_CRIT 2
32
+
33
+/** Error: error conditions */
34
+#define LOG_ERR 3
35
+
36
+/** Warning: warning conditions */
37
+#define LOG_WARNING 4
38
+
39
+/** Notice: normal but significant conditions */
40
+#define LOG_NOTICE 5
41
+
42
+/** Informational: informational messages */
43
+#define LOG_INFO 6
44
+
45
+/** Debug: debug-level messages */
46
+#define LOG_DEBUG 7
47
+
48
+/** @} */
49
+
50
+/** Do not log any messages */
51
+#define LOG_NONE -1
52
+
53
+extern void log_vprintf ( const char *fmt, va_list args );
54
+
55
+extern void __attribute__ (( format ( printf, 1, 2 ) ))
56
+log_printf ( const char *fmt, ... );
57
+
58
+/**
59
+ * Write message to system log
60
+ *
61
+ * @v priority		Message priority
62
+ * @v fmt		Format string
63
+ * @v ...		Arguments
64
+ */
65
+#define vsyslog( priority, fmt, args ) do {		\
66
+	if ( (priority) <= LOG_LEVEL ) {		\
67
+		log_vprintf ( fmt, (args) );		\
68
+	}						\
69
+	} while ( 0 )
70
+
71
+/**
72
+ * Write message to system log
73
+ *
74
+ * @v priority		Message priority
75
+ * @v fmt		Format string
76
+ * @v ...		Arguments
77
+ */
78
+#define syslog( priority, fmt, ... ) do {		\
79
+	if ( (priority) <= LOG_LEVEL ) {		\
80
+		log_printf ( fmt, ##__VA_ARGS__ );	\
81
+	}						\
82
+	} while ( 0 )
83
+
84
+#endif /* _SYSLOG_H */

Loading…
Cancel
Save