ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
EventLogger.h
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16/*
17Event Logger header file
18
19Author: Alex Reynolds
20*/
21#ifndef EVENT_LOGGER_H
22#define EVENT_LOGGER_H
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string>
27
28#include "core/clockwerkerrors.h"
29
30namespace clockwerk {
31
32 /// Define some standard messages for writing to file
33 const unsigned int DEFAULT_MAX_MSG = 280;
34
35 /// @brief Log level enumerations
36 ///
37 /// These enumerations indicate the log level at which
38 /// the event loger should log. ERROR is the lowest
39 /// bar for output, and is output in almost every case.
40 /// Debug is the most restrictive and typically will be
41 /// output only when set.
42 enum log_level_e {
43 NONE, /// No output at all
44 ERROR, /// Output in almost all cases
45 WARNING,
46 INFO,
47 DEBUG /// Most restrictive level of output
48 };
49
50 /**
51 * @brief Class to handle event and debug logging
52 *
53 * The event logger is a simple, lightweight, static class for logging events
54 * as they occur. To minimize impact, the logger uses c-style file IO techniques
55 * and c-strings.
56 *
57 * A configurable character limit, for now set to 140 characters, is enforced
58 * on logging. If you can't tweet it, you shouldn't be logging it in a simulation
59 * or embedded framework.
60 *
61 * This logging framework derived (sort of) from the Dr. Dobbs simple logging
62 * framework presented here: https:/// drdobbs.com/cpp/logging-in-c/201804215
63 */
64 class EventLogger {
65 public:
66 /// @brief Simple constructor for event logger
67 /// @param fp File pointer to write to -- set to stdout by default
68 /// @param max_size Max size of message, in characters. Set to 140 by default
69 EventLogger(FILE *fp=stdout, unsigned int max_size=DEFAULT_MAX_MSG) :
70 _fp(fp), _log_level(INFO), _max_message_size(max_size), _msg(DEFAULT_MAX_MSG, '\0') {};
71
72 /// @brief Function to set logging level for system
73 /// @param level Level to set logging to
74 void logLevel(log_level_e level) {_log_level = level;}
75
76 /// @brief Function to get log level for system
77 /// @return Log level
78 log_level_e logLevel() {return _log_level;}
79
80 /// @brief Function to write an event message
81 /// @param level The log level of the referenced message
82 /// @param time The time, written out as a string
83 /// @param message The message to be written. Must be <MAX_MESSAGE_SIZE (140 char)
84 /// @param local_msg The local level at which the message should be written out. Allows for higher local debugging
85 /// @return Error code corresponding to success/failure
86 int logMessage(log_level_e level, const std::string &message, log_level_e local_msg=NONE);
87
88 /// @brief Function to flush the file buffer
89 /// @note Unless you're running real time, you probably don't need to call this
90 /// @return Error code corresponding to success/failure
91 int flushLogs();
92 private:
93 /// Output stream to which output will be written
94 FILE *_fp;
95
96 /// Global logging level for the event logger
97 log_level_e _log_level;
98
99 /// Maximum message size, in #characters
100 unsigned int _max_message_size;
101
102 /// String variable and character array to hold message
103 std::string _msg;
104 };
105
106}
107
108#endif
Class to handle event and debug logging.
Definition EventLogger.h:64
log_level_e logLevel()
Function to get log level for system.
Definition EventLogger.h:78
int logMessage(log_level_e level, const std::string &message, log_level_e local_msg=NONE)
Function to write an event message.
Definition EventLogger.cpp:20
void logLevel(log_level_e level)
Function to set logging level for system.
Definition EventLogger.h:74
EventLogger(FILE *fp=stdout, unsigned int max_size=DEFAULT_MAX_MSG)
Simple constructor for event logger.
Definition EventLogger.h:69
int flushLogs()
Function to flush the file buffer.
Definition EventLogger.cpp:46