Skip to content

Instantly share code, notes, and snippets.

@ichramm
Created November 23, 2012 16:08
Show Gist options
  • Select an option

  • Save ichramm/4136277 to your computer and use it in GitHub Desktop.

Select an option

Save ichramm/4136277 to your computer and use it in GitHub Desktop.
Log wrapper with extended info
/*!
* \file logger.h
* \author ichramm
*/
#ifndef logger_h__
#define logger_h__
#define inconcert_log_debug(fmt, ...) if(logger::is_level_enabled(logger::LevelDebug)) \
inconcert_log(logger::LevelDebug, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
#define inconcert_log_info(fmt, ...) if(logger::is_level_enabled(logger::LevelInfo)) \
inconcert_log(logger::LevelInfo, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
#define inconcert_log_warning(fmt, ...) if(logger::is_level_enabled(logger::LevelWarn)) \
inconcert_log(logger::LevelWarn, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
#define inconcert_log_error(fmt, ...) if(logger::is_level_enabled(logger::LevelError)) \
inconcert_log(logger::LevelError, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
#define inconcert_log_fatal(fmt, ...) if(logger::is_level_enabled(logger::LevelFatal)) \
inconcert_log(logger::LevelFatal, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
#define inconcert_log_profile(fmt, ...) if(logger::is_level_enabled(logger::LevelProfile)) \
inconcert_log(logger::LevelProfile, "%s:%d %s - " fmt, logger::get_filename_part(__FILE__), __LINE__, __FUNCTION__ , ## __VA_ARGS__)
// Arguments will be type-checked against format string if format is specified
#if _MSC_VER > 1400
# include <sal.h> // use /analyze or _USE_ATTRIBUTES_FOR_SAL for checking
# define format_string(p__) _Printf_format_string_ p__
#else
# define format_string(p__) p__
#endif // _MSC_VER > 1400
#if defined(__GNUC__)
# define attribute_format(a__, b__) __attribute__((format(printf, a__, b__)))
#else
# define attribute_format(a__, b__)
#endif // defined(__GNUC__)
namespace logger
{
/*!
* All known log levels
*/
enum LogLevel
{
LevelDebug = (1 << 0),
LevelInfo = (1 << 1),
LevelWarn = (1 << 2),
LevelError = (1 << 3),
LevelFatal = (1 << 4),
LevelProfile = (1 << 5)
};
/*!
* Log function
*
* \remarks Do not use this function directly, use the macros defined above instead
*
* Available macros:
* \li inconcert_log_debug
* \li inconcert_log_info
* \li inconcert_log_warning
* \li inconcert_log_error
* \li inconcert_log_fatal
* \li inconcert_log_profile
*/
void inconcert_log (
LogLevel level,
format_string(const char* format),
...
) attribute_format(2,3);
/*!
* \return \c true if log flag \mask is enable, otherwise \c false
*/
bool is_level_enabled(unsigned int mask);
/*!
* Given \p file in the form \c /path/to/file.cpp this function returns \c file.cpp
*/
const char *get_filename_part(const char *file);
} // namespace logger
#endif // logger_h__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment