Commit cde4b01b authored by jacobgladish's avatar jacobgladish

Updated log to allow configuration.

parent e3351c74
...@@ -20,7 +20,7 @@ namespace ...@@ -20,7 +20,7 @@ namespace
adapters::mock::MockAdapter::MockAdapter() adapters::mock::MockAdapter::MockAdapter()
: m_vendor("Acme") : m_vendor("Acme")
, m_adapterName("DSB Mock Adapter") , m_adapterName("MockAdapter")
, m_exposedAdapterPrefix("com." + m_vendor) , m_exposedAdapterPrefix("com." + m_vendor)
, m_exposedApplicationName("DeviceSystemBridge") , m_exposedApplicationName("DeviceSystemBridge")
, m_exposedApplicationGuid(common::Guid::Parse("C27BC425-0058-4829-8775-441B5D8740C0")) , m_exposedApplicationGuid(common::Guid::Parse("C27BC425-0058-4829-8775-441B5D8740C0"))
......
...@@ -23,23 +23,23 @@ namespace ...@@ -23,23 +23,23 @@ namespace
void alljoynLogger(DbgMsgType type, char const* module, char const* msg, void* /*ctx*/) 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) switch (type)
{ {
case DBG_LOCAL_ERROR: case DBG_LOCAL_ERROR:
case DBG_REMOTE_ERROR: case DBG_REMOTE_ERROR:
level = common::Logger::DSB_LOGLEVEL_ERROR; level = common::LogLevel::Error;
break; break;
case DBG_HIGH_LEVEL: case DBG_HIGH_LEVEL:
level = common::Logger::DSB_LOGLEVEL_WARN; level = common::LogLevel::Warn;
break; break;
case DBG_GEN_MESSAGE: case DBG_GEN_MESSAGE:
level = common::Logger::DSB_LOGLEVEL_INFO; level = common::LogLevel::Info;
break; break;
case DBG_API_TRACE: case DBG_API_TRACE:
case DBG_REMOTE_DATA: case DBG_REMOTE_DATA:
case DBG_LOCAL_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); common::Logger::Write("alljoyn", level, NULL, 0, "[%s] %s", module, msg);
} }
...@@ -200,7 +200,7 @@ bridge::DeviceSystemBridge::InitializeAdapter() ...@@ -200,7 +200,7 @@ bridge::DeviceSystemBridge::InitializeAdapter()
return ER_FAIL; 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); int ret = m_adapter->Initialize(log);
return ret == 0 ? ER_OK : ER_FAIL; return ret == 0 ? ER_OK : ER_FAIL;
} }
......
...@@ -21,21 +21,13 @@ namespace ...@@ -21,21 +21,13 @@ namespace
if (!error) if (!error)
return; return;
common::Logger::Level level = common::Logger::DSB_LOGLEVEL_INFO; common::LogLevel level = common::LogLevel::Info;
switch (error->level) switch (error->level)
{ {
case XML_ERR_NONE: case XML_ERR_NONE: level = common::LogLevel::Debug; break;
level = common::Logger::DSB_LOGLEVEL_DEBUG; case XML_ERR_WARNING: level = common::LogLevel::Warn; break;
break; case XML_ERR_ERROR: level = common::LogLevel::Error; break;
case XML_ERR_WARNING: case XML_ERR_FATAL: level = common::LogLevel::Fatal; break;
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;
} }
// remove newline. I hate blank lines in logfiles. // remove newline. I hate blank lines in logfiles.
......
...@@ -3,17 +3,17 @@ ...@@ -3,17 +3,17 @@
namespace 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) switch (level)
{ {
case bridge::AdapterLogLevel::Off: l = common::Logger::DSB_LOGLEVEL_OFF; break; case bridge::AdapterLogLevel::Off: l = common::LogLevel::Off; break;
case bridge::AdapterLogLevel::Debug: l = common::Logger::DSB_LOGLEVEL_DEBUG; break; case bridge::AdapterLogLevel::Debug: l = common::LogLevel::Debug; break;
case bridge::AdapterLogLevel::Info: l = common::Logger::DSB_LOGLEVEL_INFO; break; case bridge::AdapterLogLevel::Info: l = common::LogLevel::Info; break;
case bridge::AdapterLogLevel::Warn: l = common::Logger::DSB_LOGLEVEL_WARN; break; case bridge::AdapterLogLevel::Warn: l = common::LogLevel::Warn; break;
case bridge::AdapterLogLevel::Error: l = common::Logger::DSB_LOGLEVEL_ERROR; break; case bridge::AdapterLogLevel::Error: l = common::LogLevel::Error; break;
case bridge::AdapterLogLevel::Fatal: l = common::Logger::DSB_LOGLEVEL_FATAL; break; case bridge::AdapterLogLevel::Fatal: l = common::LogLevel::Fatal; break;
} }
return l; return l;
} }
...@@ -36,7 +36,7 @@ bridge::AdapterLog::Write(AdapterLogLevel level, char const* file, int line, cha ...@@ -36,7 +36,7 @@ bridge::AdapterLog::Write(AdapterLogLevel level, char const* file, int line, cha
bool bool
bridge::AdapterLog::IsLevelEnabled(AdapterLogLevel level) 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); return common::Logger::IsLevelEnabled(m_module.c_str(), l);
} }
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <mutex>
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
#include <sys/syscall.h> #include <sys/syscall.h>
...@@ -18,42 +20,29 @@ namespace ...@@ -18,42 +20,29 @@ namespace
{ {
FILE* gLog = stdout; FILE* gLog = stdout;
struct StringCompare std::mutex gLogMutex;
{
bool operator()(char const* s, char const* t) const
{ return strcmp(s, t) < 0; }
};
char const* LevelToString(common::Logger::Level l) char const* LevelToString(common::LogLevel l)
{ {
char const* s = "unknown"; char const* s = "unknown";
switch (l) switch (l)
{ {
case common::Logger::DSB_LOGLEVEL_OFF: case common::LogLevel::Off: s = "OFF"; break;
s = "OFF"; case common::LogLevel::Debug: s = "DEBUG"; break;
break; case common::LogLevel::Info: s = "INFO"; break;
case common::Logger::DSB_LOGLEVEL_DEBUG: case common::LogLevel::Warn: s = "WARN"; break;
s = "DEBUG"; case common::LogLevel::Error: s = "ERROR"; break;
break; case common::LogLevel::Fatal: s = "FATAL"; 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;
default: default:
break; break;
} }
return s; return s;
} }
typedef std::map<char const*, common::Logger::Level, StringCompare> LevelMap; typedef std::map<std::string, common::LogLevel> LevelMap;
LevelMap gLevelMap; LevelMap gLevelMap;
common::Logger::Level sDefaultLoggingLevel = common::Logger::DSB_LOGLEVEL_INFO; common::LogLevel const kDefaultLoggingLevel = common::LogLevel::Info;
#if defined(__APPLE__) #if defined(__APPLE__)
#define ThreadId_FMT "%p" #define ThreadId_FMT "%p"
...@@ -67,7 +56,14 @@ namespace ...@@ -67,7 +56,14 @@ namespace
} }
void 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, ...) int line, char const* format, ...)
{ {
va_list args; va_list args;
...@@ -77,30 +73,34 @@ common::Logger::Write(char const* module, Level level, char const* file, ...@@ -77,30 +73,34 @@ common::Logger::Write(char const* module, Level level, char const* file,
} }
bool 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); LevelMap::const_iterator itr = gLevelMap.find(module);
if (itr != gLevelMap.end()) if (itr != gLevelMap.end())
minLevel = itr->second; minLevel = itr->second;
return level >= minLevel; return level >= minLevel;
} }
void 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) const char* format, va_list args)
{ {
struct tm result; struct tm result;
time_t const clock = time(0); time_t const clock = time(0);
localtime_r(&clock, &result); localtime_r(&clock, &result);
// TODO: use std:: if possible
char buff[64]; char buff[64];
size_t n = strftime(buff, sizeof(buff) - 1, "%Y-%m-%dT%T", &result); size_t n = strftime(buff, sizeof(buff) - 1, "%Y-%m-%dT%T", &result);
if (n > 0) if (n > 0)
buff[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); vfprintf(gLog, format, args);
fprintf(gLog, "\n"); fprintf(gLog, "\n");
} }
...@@ -7,33 +7,36 @@ ...@@ -7,33 +7,36 @@
#endif #endif
#include <stdarg.h> #include <stdarg.h>
#include <string>
namespace common namespace common
{ {
enum class LogLevel
{
Off,
Debug,
Info,
Warn,
Error,
Fatal
};
class Logger class Logger
{ {
public: public:
enum Level static void SetLevel(std::string const& module, LogLevel level);
{
DSB_LOGLEVEL_OFF = -1, static void Write(std::string const& module, LogLevel level, const char* file,
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,
int line, const char* format, ...) PRINTF_FORMAT(6, 7); 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, ...) \ #define DSBLOG_WITH_LEVEL(LEVEL, FORMAT, ...) \
do { if (common::Logger::IsLevelEnabled(__dsb_logger_module_name__, LEVEL)) { \ do { if (common::Logger::IsLevelEnabled(__dsb_logger_module_name__, LEVEL)) { \
...@@ -41,15 +44,15 @@ namespace common ...@@ -41,15 +44,15 @@ namespace common
} } while (0) } } while (0)
#define DSBLOG(LEVEL, NAME, FORMAT, ...) \ #define DSBLOG(LEVEL, NAME, FORMAT, ...) \
do { if (common::Logger::IsLevelEnabled(NAME, common::Logger::LEVEL)) { \ do { if (common::Logger::IsLevelEnabled(NAME, common::LogLevel::LEVEL)) { \
common::Logger::Write(NAME, common::Logger::LEVEL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__); \ common::Logger::Write(NAME, common::LogLevel::LEVEL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__); \
} } while (0) } } while (0)
#define DSBLOG_DEBUG(FORMAT, ...) DSBLOG(DSB_LOGLEVEL_DEBUG, __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(DSB_LOGLEVEL_INFO, __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(DSB_LOGLEVEL_WARN, __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(DSB_LOGLEVEL_ERROR, __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__); #define DSBLOG_NOT_IMPLEMENTED() DSBLOG_WARN("%s has not been implemented", __func__);
...@@ -65,6 +65,8 @@ int main(int /*argc*/, char* /*argv*/ []) ...@@ -65,6 +65,8 @@ int main(int /*argc*/, char* /*argv*/ [])
std::shared_ptr<bridge::IAdapter> adapter = DSB()->GetAdapter(); std::shared_ptr<bridge::IAdapter> adapter = DSB()->GetAdapter();
common::Logger::SetLevel(adapter->GetAdapterName(), common::LogLevel::Debug);
bridge::AdapterDeviceVector deviceList; bridge::AdapterDeviceVector deviceList;
std::shared_ptr<bridge::IAdapterIoRequest> req; 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