Commit cde4b01b authored by jacobgladish's avatar jacobgladish

Updated log to allow configuration.

parent e3351c74
......@@ -20,7 +20,7 @@ namespace
adapters::mock::MockAdapter::MockAdapter()
: m_vendor("Acme")
, m_adapterName("DSB Mock Adapter")
, m_adapterName("MockAdapter")
, m_exposedAdapterPrefix("com." + m_vendor)
, m_exposedApplicationName("DeviceSystemBridge")
, m_exposedApplicationGuid(common::Guid::Parse("C27BC425-0058-4829-8775-441B5D8740C0"))
......
......@@ -23,23 +23,23 @@ namespace
void alljoynLogger(DbgMsgType type, char const* module, char const* msg, void* /*ctx*/)
{
common::Logger::Level level = common::Logger::DSB_LOGLEVEL_INFO;
common::LogLevel level = common::LogLevel::Info;
switch (type)
{
case DBG_LOCAL_ERROR:
case DBG_REMOTE_ERROR:
level = common::Logger::DSB_LOGLEVEL_ERROR;
level = common::LogLevel::Error;
break;
case DBG_HIGH_LEVEL:
level = common::Logger::DSB_LOGLEVEL_WARN;
level = common::LogLevel::Warn;
break;
case DBG_GEN_MESSAGE:
level = common::Logger::DSB_LOGLEVEL_INFO;
level = common::LogLevel::Info;
break;
case DBG_API_TRACE:
case DBG_REMOTE_DATA:
case DBG_LOCAL_DATA:
level = common::Logger::DSB_LOGLEVEL_DEBUG;
level = common::LogLevel::Debug;
}
common::Logger::Write("alljoyn", level, NULL, 0, "[%s] %s", module, msg);
}
......@@ -200,7 +200,7 @@ bridge::DeviceSystemBridge::InitializeAdapter()
return ER_FAIL;
}
std::shared_ptr<IAdapterLog> log(new AdapterLog("adapter"));
std::shared_ptr<IAdapterLog> log(new AdapterLog(m_adapter->GetAdapterName()));
int ret = m_adapter->Initialize(log);
return ret == 0 ? ER_OK : ER_FAIL;
}
......
......@@ -21,21 +21,13 @@ namespace
if (!error)
return;
common::Logger::Level level = common::Logger::DSB_LOGLEVEL_INFO;
common::LogLevel level = common::LogLevel::Info;
switch (error->level)
{
case XML_ERR_NONE:
level = common::Logger::DSB_LOGLEVEL_DEBUG;
break;
case XML_ERR_WARNING:
level = common::Logger::DSB_LOGLEVEL_WARN;
break;
case XML_ERR_ERROR:
level = common::Logger::DSB_LOGLEVEL_ERROR;
break;
case XML_ERR_FATAL:
level = common::Logger::DSB_LOGLEVEL_FATAL;
break;
case XML_ERR_NONE: level = common::LogLevel::Debug; break;
case XML_ERR_WARNING: level = common::LogLevel::Warn; break;
case XML_ERR_ERROR: level = common::LogLevel::Error; break;
case XML_ERR_FATAL: level = common::LogLevel::Fatal; break;
}
// remove newline. I hate blank lines in logfiles.
......
......@@ -3,17 +3,17 @@
namespace
{
common::Logger::Level toLogLevel(bridge::AdapterLogLevel level)
common::LogLevel toLogLevel(bridge::AdapterLogLevel level)
{
common::Logger::Level l = common::Logger::DSB_LOGLEVEL_OFF;
common::LogLevel l = common::LogLevel::Off;
switch (level)
{
case bridge::AdapterLogLevel::Off: l = common::Logger::DSB_LOGLEVEL_OFF; break;
case bridge::AdapterLogLevel::Debug: l = common::Logger::DSB_LOGLEVEL_DEBUG; break;
case bridge::AdapterLogLevel::Info: l = common::Logger::DSB_LOGLEVEL_INFO; break;
case bridge::AdapterLogLevel::Warn: l = common::Logger::DSB_LOGLEVEL_WARN; break;
case bridge::AdapterLogLevel::Error: l = common::Logger::DSB_LOGLEVEL_ERROR; break;
case bridge::AdapterLogLevel::Fatal: l = common::Logger::DSB_LOGLEVEL_FATAL; break;
case bridge::AdapterLogLevel::Off: l = common::LogLevel::Off; break;
case bridge::AdapterLogLevel::Debug: l = common::LogLevel::Debug; break;
case bridge::AdapterLogLevel::Info: l = common::LogLevel::Info; break;
case bridge::AdapterLogLevel::Warn: l = common::LogLevel::Warn; break;
case bridge::AdapterLogLevel::Error: l = common::LogLevel::Error; break;
case bridge::AdapterLogLevel::Fatal: l = common::LogLevel::Fatal; break;
}
return l;
}
......@@ -36,7 +36,7 @@ bridge::AdapterLog::Write(AdapterLogLevel level, char const* file, int line, cha
bool
bridge::AdapterLog::IsLevelEnabled(AdapterLogLevel level)
{
common::Logger::Level l = toLogLevel(level);
common::LogLevel l = toLogLevel(level);
return common::Logger::IsLevelEnabled(m_module.c_str(), l);
}
......@@ -7,6 +7,8 @@
#include <sys/time.h>
#include <time.h>
#include <mutex>
#ifdef __linux__
#include <unistd.h>
#include <sys/syscall.h>
......@@ -18,42 +20,29 @@ namespace
{
FILE* gLog = stdout;
struct StringCompare
{
bool operator()(char const* s, char const* t) const
{ return strcmp(s, t) < 0; }
};
std::mutex gLogMutex;
char const* LevelToString(common::Logger::Level l)
char const* LevelToString(common::LogLevel l)
{
char const* s = "unknown";
switch (l)
{
case common::Logger::DSB_LOGLEVEL_OFF:
s = "OFF";
break;
case common::Logger::DSB_LOGLEVEL_DEBUG:
s = "DEBUG";
break;
case common::Logger::DSB_LOGLEVEL_INFO:
s = "INFO";
break;
case common::Logger::DSB_LOGLEVEL_WARN:
s = "WARN";
break;
case common::Logger::DSB_LOGLEVEL_ERROR:
s = "ERROR";
break;
case common::LogLevel::Off: s = "OFF"; break;
case common::LogLevel::Debug: s = "DEBUG"; break;
case common::LogLevel::Info: s = "INFO"; break;
case common::LogLevel::Warn: s = "WARN"; break;
case common::LogLevel::Error: s = "ERROR"; break;
case common::LogLevel::Fatal: s = "FATAL"; break;
default:
break;
}
return s;
}
typedef std::map<char const*, common::Logger::Level, StringCompare> LevelMap;
typedef std::map<std::string, common::LogLevel> LevelMap;
LevelMap gLevelMap;
common::Logger::Level sDefaultLoggingLevel = common::Logger::DSB_LOGLEVEL_INFO;
common::LogLevel const kDefaultLoggingLevel = common::LogLevel::Info;
#if defined(__APPLE__)
#define ThreadId_FMT "%p"
......@@ -67,7 +56,14 @@ namespace
}
void
common::Logger::Write(char const* module, Level level, char const* file,
common::Logger::SetLevel(std::string const& module, LogLevel level)
{
// TODO: does this need to be thread-safe?
gLevelMap[module] = level;
}
void
common::Logger::Write(std::string const& module, LogLevel level, char const* file,
int line, char const* format, ...)
{
va_list args;
......@@ -77,30 +73,34 @@ common::Logger::Write(char const* module, Level level, char const* file,
}
bool
common::Logger::IsLevelEnabled(char const* module, Level level)
common::Logger::IsLevelEnabled(std::string const& module, LogLevel level)
{
common::Logger::Level minLevel = sDefaultLoggingLevel;
common::LogLevel minLevel = kDefaultLoggingLevel;
LevelMap::const_iterator itr = gLevelMap.find(module);
if (itr != gLevelMap.end())
minLevel = itr->second;
return level >= minLevel;
}
void
common::Logger::VaLog(const char* module, Level level, const char* file, int line,
common::Logger::VaLog(std::string const& module, LogLevel level, const char* file, int line,
const char* format, va_list args)
{
struct tm result;
time_t const clock = time(0);
localtime_r(&clock, &result);
// TODO: use std:: if possible
char buff[64];
size_t n = strftime(buff, sizeof(buff) - 1, "%Y-%m-%dT%T", &result);
if (n > 0)
buff[n] = '\0';
fprintf(gLog, "%s (%5s) Thread-" ThreadId_FMT " [%s:%s:%d] - ", buff, LevelToString(level), GetCurrentThreadId(), module, file, line);
// The logger is passed around as a shared_ptr<> to the adapter. There's no telling
// if multiple threads will log to this thing or not. synchronize to be safe
std::lock_guard<std::mutex> lock(gLogMutex);
fprintf(gLog, "%s (%5s) Thread-" ThreadId_FMT " [%s:%s:%d] - ", buff, LevelToString(level),
GetCurrentThreadId(), module.c_str(), file, line);
vfprintf(gLog, format, args);
fprintf(gLog, "\n");
}
......@@ -7,33 +7,36 @@
#endif
#include <stdarg.h>
#include <string>
namespace common
{
enum class LogLevel
{
Off,
Debug,
Info,
Warn,
Error,
Fatal
};
class Logger
{
public:
enum Level
{
DSB_LOGLEVEL_OFF = -1,
DSB_LOGLEVEL_DEBUG = 1,
DSB_LOGLEVEL_INFO = 2,
DSB_LOGLEVEL_WARN = 3,
DSB_LOGLEVEL_ERROR = 4,
DSB_LOGLEVEL_FATAL = 5
};
static void Write(char const* module, Level level, const char* file,
static void SetLevel(std::string const& module, LogLevel level);
static void Write(std::string const& module, LogLevel level, const char* file,
int line, const char* format, ...) PRINTF_FORMAT(6, 7);
static bool IsLevelEnabled(const char* module, Level level);
static bool IsLevelEnabled(std::string const& module, LogLevel level);
static void VaLog(const char* module, Level level, const char* file, int line, const char* format, va_list args);
static void VaLog(std::string const& module, LogLevel level, const char* file,
int line, const char* format, va_list args);
};
}
#define DSB_DECLARE_LOGNAME(LOGNAME) char const __dsb_logger_module_name__[] = { #LOGNAME };
#define DSB_DECLARE_LOGNAME(LOGNAME) std::string const __dsb_logger_module_name__ = #LOGNAME
#define DSBLOG_WITH_LEVEL(LEVEL, FORMAT, ...) \
do { if (common::Logger::IsLevelEnabled(__dsb_logger_module_name__, LEVEL)) { \
......@@ -41,15 +44,15 @@ namespace common
} } while (0)
#define DSBLOG(LEVEL, NAME, FORMAT, ...) \
do { if (common::Logger::IsLevelEnabled(NAME, common::Logger::LEVEL)) { \
common::Logger::Write(NAME, common::Logger::LEVEL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__); \
do { if (common::Logger::IsLevelEnabled(NAME, common::LogLevel::LEVEL)) { \
common::Logger::Write(NAME, common::LogLevel::LEVEL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__); \
} } while (0)
#define DSBLOG_DEBUG(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_DEBUG, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_INFO(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_INFO, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_WARN(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_WARN, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_ERROR(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_ERROR, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_DEBUG(FORMAT, ...) DSBLOG(Debug, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_INFO(FORMAT, ...) DSBLOG(Info, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_WARN(FORMAT, ...) DSBLOG(Warn, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_ERROR(FORMAT, ...) DSBLOG(Error, __dsb_logger_module_name__, FORMAT, ##__VA_ARGS__)
#define DSBLOG_NOT_IMPLEMENTED() DSBLOG_WARN("%s has not been implemented", __func__);
......@@ -65,6 +65,8 @@ int main(int /*argc*/, char* /*argv*/ [])
std::shared_ptr<bridge::IAdapter> adapter = DSB()->GetAdapter();
common::Logger::SetLevel(adapter->GetAdapterName(), common::LogLevel::Debug);
bridge::AdapterDeviceVector deviceList;
std::shared_ptr<bridge::IAdapterIoRequest> req;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment