Moved sources to src dir
This commit is contained in:
75
src/log4cpp.cpp
Normal file
75
src/log4cpp.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* log4cpp.cpp
|
||||
*
|
||||
* Created by Bob Polis on 05-07-2007.
|
||||
* Copyright 2007 Thalictrum. All rights reserved.
|
||||
*/
|
||||
|
||||
#include "log4cpp.hpp"
|
||||
|
||||
// UNIX
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
// C++ Standard Library
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
using namespace std;
|
||||
|
||||
stringstream glog;
|
||||
pthread_mutex_t log4cpp_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
mutex m;
|
||||
|
||||
void print_timestamp()
|
||||
{
|
||||
// retrieve accurate time for milliseconds display
|
||||
struct timeval secs;
|
||||
(void)::gettimeofday(&secs, nullptr);
|
||||
int ms = static_cast<int>(std::roundf(secs.tv_usec / 1000.0)) % 1000;
|
||||
|
||||
// create a date/time stamp
|
||||
char dtstamp[20];
|
||||
std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec));
|
||||
|
||||
std::cerr << dtstamp << "." << std::setw(3) << ms << " ";
|
||||
#if __APPLE__
|
||||
std::cerr << getprogname() << "[" << getpid() << "] ";
|
||||
#elif __linux__
|
||||
std::cerr << program_invocation_name << "[" << getpid() << "] ";
|
||||
#endif
|
||||
}
|
||||
|
||||
void __slog(const char* func, const char* file, int line, stringstream& ss)
|
||||
{
|
||||
::pthread_mutex_lock(&log4cpp_lock);
|
||||
print_timestamp();
|
||||
string f(file);
|
||||
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << ss.str() << endl;
|
||||
ss.str("");
|
||||
::pthread_mutex_unlock(&log4cpp_lock);
|
||||
}
|
||||
|
||||
void _dlog(const char* func, const char* file, int line, const char* fmt, ...) {
|
||||
lock_guard<mutex> lock {m};
|
||||
print_timestamp();
|
||||
string f {file};
|
||||
va_list args1;
|
||||
va_start(args1, fmt);
|
||||
va_list args2;
|
||||
va_copy(args2, args1);
|
||||
vector<char> buf(1 + vsnprintf(nullptr, 0, fmt, args1));
|
||||
va_end(args1);
|
||||
vsnprintf(buf.data(), buf.size(), fmt, args2);
|
||||
va_end(args2);
|
||||
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << buf.data() << endl;
|
||||
}
|
30
src/log4cpp.hpp
Normal file
30
src/log4cpp.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* log4cpp.h
|
||||
*
|
||||
* Created by Bob Polis on 05-07-2007.
|
||||
* Copyright 2007 Thalictrum. All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef __log4cpp__
|
||||
#define __log4cpp__
|
||||
|
||||
#include <sstream>
|
||||
#include <pthread.h>
|
||||
|
||||
extern std::stringstream glog; // WARNING: *not* thread-safe!
|
||||
|
||||
void print_timestamp();
|
||||
void __slog(const char* func, const char* file, int line, std::stringstream& ss);
|
||||
void _dlog(const char* func, const char* file, int line, const char* fmt, ...);
|
||||
|
||||
#if DEBUG
|
||||
#define _slog(ss) __slog(__FUNCTION__, __FILE__, __LINE__, (std::stringstream&)(ss));
|
||||
#define dlog(fmt, ...) _dlog(__FUNCTION__, __FILE__, __LINE__, fmt, __VA_ARGS__);
|
||||
#else
|
||||
#define _slog(ss)
|
||||
#define dlog(fmt, ...)
|
||||
#endif
|
||||
|
||||
#define sclog(fmt, ...) _dlog(__FUNCTION__, __FILE__, __LINE__, fmt, __VA_ARGS__);
|
||||
|
||||
#endif //__log4cpp__
|
46
src/logger.cpp
Normal file
46
src/logger.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// logger.cpp
|
||||
// libsclogging
|
||||
//
|
||||
// Created by Bob Polis at 2020-04-29
|
||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||
//
|
||||
|
||||
#include "logger.hpp"
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace sc;
|
||||
|
||||
logger::logger(const std::string& name, loglevel level)
|
||||
: _level(level), _name(name) {}
|
||||
|
||||
void logger::msg(loglevel level, const char* file, int line, const char* func) {
|
||||
if (level >= _level) {
|
||||
if (_topcall) {
|
||||
write_prefix(level, file, line, func);
|
||||
} else {
|
||||
_topcall = true;
|
||||
}
|
||||
std::cerr << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void logger::write_prefix(loglevel level, const char* file, int line, const char* func) {
|
||||
// retrieve accurate time for milliseconds display
|
||||
struct timeval secs;
|
||||
(void)::gettimeofday(&secs, nullptr);
|
||||
int ms = static_cast<int>(std::roundf(secs.tv_usec / 1000.0)) % 1000;
|
||||
|
||||
// create a date/time stamp
|
||||
char dtstamp[20];
|
||||
std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec));
|
||||
|
||||
std::cerr << dtstamp << "." << std::setfill('0') << std::setw(3) << ms << " ";
|
||||
std::cerr << _name << "[" << ::getpid() << "] ";
|
||||
std::cerr << file << ":" << line << " " << func << "() ";
|
||||
std::cerr << '[' << loglevel_desc(level) << "] ";
|
||||
}
|
83
src/logger.hpp
Normal file
83
src/logger.hpp
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// logger.hpp
|
||||
// libsclogging
|
||||
//
|
||||
// Created by Bob Polis at 2020-04-29
|
||||
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef _logger_H_
|
||||
#define _logger_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace sc {
|
||||
|
||||
enum class loglevel {
|
||||
undefined,
|
||||
debug,
|
||||
info,
|
||||
warning,
|
||||
error,
|
||||
critical
|
||||
};
|
||||
|
||||
constexpr bool operator>=(loglevel lhs, loglevel rhs) {
|
||||
return static_cast<int>(lhs) >= static_cast<int>(rhs);
|
||||
}
|
||||
|
||||
constexpr const char* loglevel_desc(loglevel lvl) {
|
||||
switch (lvl) {
|
||||
case loglevel::undefined: return "UNDEFINED"; break;
|
||||
case loglevel::debug: return "DEBUG"; break;
|
||||
case loglevel::info: return "INFO"; break;
|
||||
case loglevel::warning: return "WARNING"; break;
|
||||
case loglevel::error: return "ERROR"; break;
|
||||
case loglevel::critical: return "CRITICAL"; break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
class logger {
|
||||
public:
|
||||
logger(const std::string& name, loglevel level);
|
||||
|
||||
void msg(loglevel level, const char* file, int line, const char* func);
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void msg(loglevel level, const char* file, int line, const char* func,
|
||||
T value, Args... args) {
|
||||
if (level >= _level) {
|
||||
if (_topcall) {
|
||||
_topcall = false;
|
||||
write_prefix(level, file, line, func);
|
||||
}
|
||||
std::cerr << value;
|
||||
msg(level, file, line, func, args...);
|
||||
}
|
||||
}
|
||||
|
||||
loglevel level() const { return _level; }
|
||||
void level(loglevel level) { _level = level; }
|
||||
|
||||
private:
|
||||
void write_prefix(loglevel level,
|
||||
const char* file,
|
||||
int line,
|
||||
const char* func);
|
||||
|
||||
loglevel _level {loglevel::info};
|
||||
bool _topcall {true};
|
||||
std::string _name;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define SCDebug(logger, ...) logger.msg(sc::loglevel::debug, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define SCInfo(logger, ...) logger.msg(sc::loglevel::info, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define SCWarning(logger, ...) logger.msg(sc::loglevel::warning, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define SCError(logger, ...) logger.msg(sc::loglevel::error, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define SCCritical(logger, ...) logger.msg(sc::loglevel::critical, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
|
||||
#endif // _logger_H_
|
Reference in New Issue
Block a user