Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit a2efee3

Browse files
committed
Merge pull request #199 from swift-nav/mfine-logging
log_ logging
2 parents 621f40f + 599cd54 commit a2efee3

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

include/libswiftnav/logging.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include <stdio.h>
1717

18+
#include "common.h"
19+
1820
/* DEBUG off by default, enable it on a per-file basis. */
1921
#ifndef DEBUG
2022
#define DEBUG 0
@@ -31,28 +33,59 @@
3133
*
3234
* \{ */
3335

36+
extern void log_(u8 level, const char *msg, ...) __attribute__ ((weak));
37+
38+
#define LOG_EMERG 0 /* system is unusable */
39+
#define LOG_ALERT 1 /* action must be taken immediately */
40+
#define LOG_CRIT 2 /* critical conditions */
41+
#define LOG_ERROR 3 /* error conditions */
42+
#define LOG_WARN 4 /* warning conditions */
43+
#define LOG_NOTICE 5 /* normal but significant condition */
44+
#define LOG_INFO 6 /* informational */
45+
#define LOG_DEBUG 7 /* debug-level messages */
46+
47+
/** Log an emergency.
48+
* \param args `printf` style format and arguments.
49+
*/
50+
#define log_emerg(args...) log_(LOG_EMERG, args)
51+
52+
/** Log an alert.
53+
* \param args `printf` style format and arguments.
54+
*/
55+
#define log_alert(args...) log_(LOG_ALERT, args)
56+
57+
/** Log a critical event.
58+
* \param args `printf` style format and arguments.
59+
*/
60+
#define log_crit(args...) log_(LOG_CRIT, args)
61+
3462
/** Log an error.
3563
* \param args `printf` style format and arguments.
3664
*/
37-
#define log_error(args...) printf("ERROR: " args)
65+
#define log_error(args...) log_(LOG_ERROR, args)
3866

3967
/** Log a warning.
4068
* \param args `printf` style format and arguments.
4169
*/
42-
#define log_warn(args...) printf("WARNING: " args)
70+
#define log_warn(args...) log_(LOG_WARN, args)
71+
72+
/** Log a notice.
73+
* \param args `printf` style format and arguments.
74+
*/
75+
#define log_notice(args...) log_(LOG_NOTICE, args)
4376

4477
/** Log an information message.
4578
* \param args `printf` style format and arguments.
4679
*/
47-
#define log_info(args...) printf("INFO: " args)
80+
#define log_info(args...) log_(LOG_INFO, args)
4881

4982
/** Log a debugging message.
5083
* \param args `printf` style format and arguments.
5184
*/
5285
#define log_debug(args...) \
5386
do { \
5487
if (DEBUG) { \
55-
printf("DEBUG: " args); \
88+
log_(LOG_DEBUG, args); \
5689
} \
5790
} while (0)
5891

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set_source_files_properties(${plover_SRCS} PROPERTIES GENERATED TRUE)
1515
set_source_files_properties(${plover_HDRS} PROPERTIES GENERATED TRUE)
1616

1717
set(libswiftnav_SRCS
18+
logging.c
1819
ephemeris.c
1920
nav_msg.c
2021
pvt.c

src/logging.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2015 Swift Navigation Inc.
3+
* Contact: Fergus Noble <fergus@swift-nav.com>
4+
*
5+
* This source is subject to the license found in the file 'LICENSE' which must
6+
* be be distributed together with this source. All other rights reserved.
7+
*
8+
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
9+
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
10+
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
11+
*/
12+
13+
#include <stdarg.h>
14+
15+
#include "logging.h"
16+
17+
/** \defgroup logging Logging
18+
* Logging functions.
19+
* \{ */
20+
21+
const char *level_string[] = {
22+
"EMERGENCY",
23+
"ALERT",
24+
"CRITICAL",
25+
"ERROR",
26+
"WARNING",
27+
"NOTICE",
28+
"INFO",
29+
"DEBUG",
30+
};
31+
32+
/** Log message by level.
33+
*
34+
* \param level Log level
35+
* \param msg Log contents
36+
*/
37+
void log_(u8 level, const char *msg, ...)
38+
{
39+
va_list ap;
40+
41+
printf("%s: ", level_string[level]);
42+
va_start(ap, msg);
43+
vprintf(msg, ap);
44+
va_end(ap);
45+
}
46+
47+
/* \} */

0 commit comments

Comments
 (0)