Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
logging.h File Reference
#include <stdio.h>
#include <inttypes.h>
#include "os/os.h"

Go to the source code of this file.

Macros

#define __STDC_VERSION__   199901L
 Hack to make vsnprintf show up.
 
#define LOCK_LOG
 Called before starting to use the logging buffer.
 
#define UNLOCK_LOG
 Called after the log buffer is flushed.
 
#define GLOBAL_LOG_OUTPUT   log_output
 Used for writing log buffersto the log output.
 
#define LOG_MAYBE_DIE(level)   0
 Splits the death behavior of FreeRTOS (where we just blink) from everything else (where we actually print a death message).
 
#define LOG(level, message...)
 Conditionally write a message to the logging output.
 
#define LOG_ERROR(message...)   LOG(LEVEL_ERROR, message)
 Shorthand for LOG(LEVEL_ERROR, message...). See LOG.
 
#define LOGLEVEL   INFO
 Default loglevel.
 
#define ERRNOCHECK(where, x...)
 Calls the function x, and if the return value is negative, prints errno as error message to stderr and exits the current program.
 

Functions

void log_output (char *buf, int size)
 Prints a line of log to the log output destination.
 
void print_errno_and_exit (const char *where)
 Prints an error message about errno to std error and terminates the current program.
 

Variables

static const int ALWAYS = -1
 Loglevel that is always printed.
 
static const int FATAL = 0
 Loglevel that kills the current process.
 
static const int LEVEL_ERROR = 1
 Loglevel that is always printed, reporting an error.
 
static const int WARNING = 2
 Loglevel that is always printed, reporting a warning or a retryable error.
 
static const int INFO = 3
 Loglevel that is printed by default, reporting some status information.
 
static const int VERBOSE = 4
 Loglevel that is usually not printed, reporting debugging information.
 
char logbuffer [256]
 Temporary buffer to sprintf() the log lines into.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Facility to do debug printf's on a configurable loglevel.

Author
Balazs Racz
Date
3 August 2013

Definition in file logging.h.

Macro Definition Documentation

◆ __STDC_VERSION__

#define __STDC_VERSION__   199901L

Hack to make vsnprintf show up.

Definition at line 40 of file logging.h.

◆ ERRNOCHECK

#define ERRNOCHECK (   where,
  x... 
)
Value:
do \
{ \
if ((x) < 0) \
{ \
print_errno_and_exit(where); \
} \
} while (0)

Calls the function x, and if the return value is negative, prints errno as error message to stderr and exits the current program.

Parameters
whereis a string, describing the operation that's being executed.
xis an expression, usually a system function call with arguments that needsto be run and the return value checked.

example usage: ERRNOCHECK("file close", close(fd_));

Definition at line 174 of file logging.h.

◆ GLOBAL_LOG_OUTPUT

#define GLOBAL_LOG_OUTPUT   log_output

Used for writing log buffersto the log output.

Hack to make this compile under C and C++ both.

Definition at line 81 of file logging.h.

◆ LOCK_LOG

#define LOCK_LOG

Called before starting to use the logging buffer.

Definition at line 71 of file logging.h.

◆ LOG

#define LOG (   level,
  message... 
)
Value:
do \
{ \
if (LOG_MAYBE_DIE(level)) \
{ \
DIE("log fatal"); \
} \
else if (level == FATAL) \
{ \
fprintf(stderr, message); \
fprintf(stderr, "\n"); \
abort(); \
} \
else if (LOGLEVEL >= level) \
{ \
int sret = snprintf(logbuffer, sizeof(logbuffer), message); \
if (sret > (int)sizeof(logbuffer)) \
sret = sizeof(logbuffer); \
GLOBAL_LOG_OUTPUT(logbuffer, sret); \
} \
} while (0)
#define LOG_MAYBE_DIE(level)
Splits the death behavior of FreeRTOS (where we just blink) from everything else (where we actually p...
Definition logging.h:89
#define LOCK_LOG
Called before starting to use the logging buffer.
Definition logging.h:71
#define UNLOCK_LOG
Called after the log buffer is flushed.
Definition logging.h:73
#define LOGLEVEL
Default loglevel.
Definition logging.h:140
char logbuffer[256]
Temporary buffer to sprintf() the log lines into.
Definition logging.cxx:42
static const int FATAL
Loglevel that kills the current process.
Definition logging.h:51

Conditionally write a message to the logging output.

Parameters
levelis the log level; if the configured loglevel is smaller, then the log is not printed, not rendered, and the rendering code is never even compiled. This makes it cheap to have LOG(VERBOSE, ...) messages left in the code everywhere.
messageis a printf format argument and possibly more arguments that are referenced from the printf format.

Definition at line 99 of file logging.h.

◆ LOG_ERROR

#define LOG_ERROR (   message...)    LOG(LEVEL_ERROR, message)

Shorthand for LOG(LEVEL_ERROR, message...). See LOG.

Definition at line 124 of file logging.h.

◆ LOG_MAYBE_DIE

#define LOG_MAYBE_DIE (   level)    0

Splits the death behavior of FreeRTOS (where we just blink) from everything else (where we actually print a death message).

Definition at line 89 of file logging.h.

◆ LOGLEVEL

#define LOGLEVEL   INFO

Default loglevel.

Definition at line 140 of file logging.h.

◆ UNLOCK_LOG

#define UNLOCK_LOG

Called after the log buffer is flushed.

Definition at line 73 of file logging.h.

Function Documentation

◆ log_output()

void log_output ( char *  buf,
int  size 
)

Prints a line of log to the log output destination.

Parameters
bufis the logging buffer. Guaranteed to be available until this function returns.
sizeis now many bytes are there in the logging buffer. There is never a terminating
in the log buffer. There is a terminating zero at buf[size].

Prints a line of log to the log output destination.

This is a definition fo a C++ symbol, and should be included exactly once into one .cxx file; typicall into main.cxx.

Parameters
bufpointer to bytes to log. Does not contain a
.
sizehow may bytes to log.

Definition at line 73 of file logging.cxx.

◆ print_errno_and_exit()

void print_errno_and_exit ( const char *  where)

Prints an error message about errno to std error and terminates the current program.

Parameters
whereC-style string describing where the error has happened. Will be printed too.

Definition at line 47 of file errno_exit.c.

Variable Documentation

◆ ALWAYS

const int ALWAYS = -1
static

Loglevel that is always printed.

Definition at line 49 of file logging.h.

◆ FATAL

const int FATAL = 0
static

Loglevel that kills the current process.

Definition at line 51 of file logging.h.

◆ INFO

const int INFO = 3
static

Loglevel that is printed by default, reporting some status information.

Definition at line 57 of file logging.h.

◆ LEVEL_ERROR

const int LEVEL_ERROR = 1
static

Loglevel that is always printed, reporting an error.

Definition at line 53 of file logging.h.

◆ logbuffer

char logbuffer[256]
extern

Temporary buffer to sprintf() the log lines into.

Definition at line 42 of file logging.cxx.

◆ VERBOSE

const int VERBOSE = 4
static

Loglevel that is usually not printed, reporting debugging information.

Definition at line 59 of file logging.h.

◆ WARNING

const int WARNING = 2
static

Loglevel that is always printed, reporting a warning or a retryable error.

Definition at line 55 of file logging.h.