Created
May 21, 2021 16:10
-
-
Save winwinashwin/bda5d6d3f77cb72bdfa591c7419de946 to your computer and use it in GitHub Desktop.
Simple and macrofied logging | C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file macrologger.hpp | |
* | |
* @brief Quick and simple logging for C++ projects | |
* | |
* @author David Rodrigues (primary author) | |
* @author Ashwin A Nayar | |
* | |
* Add compile flag -DLOG_LEVEL=<value> to set log level | |
* | |
* Log Level | Value | |
* =================== | |
* No logs 0 | |
* Error 1 | |
* Warning 2 | |
* Info 3 | |
* Debug 4 (default) | |
*/ | |
//////////////////////////////////////////////////////////////////////////////////// | |
/* | |
* Copyright (c) 2012 David Rodrigues | |
* Copyright (c) 2021 Ashwin A Nayar | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a | |
* copy of this software and associated documentation files (the "Software"), | |
* to deal in the Software without restriction, including without limitation | |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
* and/or sell copies of the Software, and to permit persons to whom the | |
* Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included | |
* in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
* IN THE SOFTWARE. | |
*/ | |
//////////////////////////////////////////////////////////////////////////////////// | |
#ifndef GISTS_MACROLOGGER_HPP | |
#define GISTS_MACROLOGGER_HPP | |
#include <cstring> | |
#include <ctime> | |
//! Auxiliary functions | |
static inline char *timenow(); | |
#define SRC_FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ | |
#define NO_LOGS 0x00 | |
#define ERROR_LEVEL 0x01 | |
#define WARN_LEVEL 0x02 | |
#define INFO_LEVEL 0x03 | |
#define DEBUG_LEVEL 0x04 | |
#ifndef LOG_LEVEL | |
#define LOG_LEVEL DEBUG_LEVEL | |
#endif | |
#define PRINTFUNCTION(format, ...) fprintf(stderr, format, __VA_ARGS__) | |
#define LOG_FMT "%s | %-5s | %-15s | %-12s:%-3d | " | |
#define LOG_ARGS(LOG_TAG) timenow(), LOG_TAG, SRC_FILE, __FUNCTION__, __LINE__ | |
#define NEWLINE "\n" | |
#define ERROR_TAG "ERROR" | |
#define WARN_TAG "WARN" | |
#define INFO_TAG "INFO" | |
#define DEBUG_TAG "DEBUG" | |
#if LOG_LEVEL >= DEBUG_LEVEL | |
#define LOG_DEBUG(message, ...) \ | |
PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(DEBUG_TAG), ##__VA_ARGS__) | |
#else | |
#define LOG_DEBUG(message, ...) | |
#endif | |
#if LOG_LEVEL >= INFO_LEVEL | |
#define LOG_INFO(message, ...) \ | |
PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(INFO_TAG), ##__VA_ARGS__) | |
#else | |
#define LOG_INFO(message, ...) | |
#endif | |
#if LOG_LEVEL >= WARN_LEVEL | |
#define LOG_WARN(message, ...) \ | |
PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(WARN_TAG), ##__VA_ARGS__) | |
#else | |
#define LOG_WARN(message, ...) | |
#endif | |
#if LOG_LEVEL >= ERROR_LEVEL | |
#define LOG_ERROR(message, ...) \ | |
PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(ERROR_TAG), ##__VA_ARGS__) | |
#else | |
#define LOG_ERROR(message, ...) | |
#endif | |
#if LOG_LEVEL >= NO_LOGS | |
#define LOG_IF_ERROR(condition, message, ...) \ | |
if (condition) \ | |
PRINTFUNCTION(LOG_FMT message NEWLINE, LOG_ARGS(ERROR_TAG), __VA_ARGS__) | |
#else | |
#define LOG_IF_ERROR(condition, message, args...) | |
#endif | |
static inline char *timenow() | |
{ | |
static char buffer[64]; | |
time_t rawtime; | |
struct tm *timeinfo; | |
time(&rawtime); | |
timeinfo = localtime(&rawtime); | |
strftime(buffer, 64, "%Y-%m-%d %H:%M:%S", timeinfo); | |
return buffer; | |
} | |
#endif // GISTS_MACROLOGGER_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment