commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
/examples/coding/Logs.cpp

Log sender

//
// Copyright (c) 2006-2014 Raphael David / CANTOR
//
#include "commore/Error.h"
#include "commore/Tuple.h"
#include <stdio.h>
//
// This is an example of how to use the log macro en log hook class
// Set dynamicaly log flag
//
CMR_MODULE_DECLARE("EXAMPLE_LOGS");
//
// Declare default compilation unit log flag
//
//
// Declare a specific log flag
//
using namespace std;
using namespace commore;
//
// A Simple loog hook
//
struct MyLogHookImpl : public LogHook
{
ListTuple messages;
//
// Hook content must be procected. message call can be issued from different thread.
//
CriticalSection messages_cs;
MyLogHookImpl()
{
//
// Activate hook
//
hook();
}
//
// Message hook function
//
void message(const LogMessage& message)
{
printf("My log hook message:level=%d key=%s\n", message.get_int("level"), (const char*)message.get_astring("key"));
}
//
// Needed for automatic autoref bindind
//
static AutoRefBase auto_create() { return new MyLogHookImpl(); }
};
typedef AutoRef<MyLogHookImpl> MyLogHook;
int main(int argc, const char* argv[])
{
int r = 0;
int num = 0;
init();
ConsoleLogHook console_log_hook(true);
//
// List all CMR log flags
//
ListSymbol names;
GlobVarLog::get_names("CMR", names);
cmr_foreach(Symbol, name, names)
{
printf(" log:%s\n", name.get_name().c_str());
}
MyLogHook my_hook;
//GlobVarLog::set_default(10);
CMR_INFO("Start example Logs");
int c = 'h';
//
// Wait '.' is typed on the keyboard
//
while (c != '.')
{
if (c == 'h')
{
printf("h : this message\n");
printf("l : create a LOG message\n");
printf("L : create a LOG message on MYLOG flag\n");
printf("2 : create a LOG2 message\n");
printf("3 : create a LOG3 message\n");
printf("e : create a ERR message\n");
printf("w : create a WARNING message\n");
printf("i : create a INFO message\n");
printf("u : UP LOG level\n");
printf("d : DOWN LOG level\n");
printf("U : UP MYLOG LOG level\n");
printf("d : DOWN MYLOG LOG level\n");
} else if (c == 'l')
{
CMR_LOG("This is a log message num: %0") << num++;
} else if (c == 'L')
{
CMR_NLOG(MYLOG, "This is a log message num: %0") << num++;
} else if (c == '2')
{
CMR_LOG2("This is a log message num: %0") << num++;
} else if (c == '3')
{
CMR_LOG3("This is a log message num: %0") << num++;
} else if (c == 'e')
{
CMR_ERROR("This is an error message num: %0") << num++;
} else if (c == 'w')
{
CMR_WARNING("This is a warning message num: %0") << num++;
} else if (c == 'i')
{
CMR_INFO("This is an info message num: %0") << num++;
} else if (c == 'u')
{
GlobVarLog::set("CMR", "LOGS", GlobVarLog::get("CMR", "LOGS") +1);
CMR_INFO("Log level set to %0") << GlobVarLog::get("CMR", "LOGS");
} else if (c == 'U')
{
GlobVarLog::set("CMR", "MYLOG", GlobVarLog::get("CMR", "MYLOG") +1);
CMR_INFO("MYLOG Log level set to %0") << GlobVarLog::get("CMR", "MYLOG");
} else if (c == 'd')
{
if (GlobVarLog::get("CMR", "LOGS") > 0)
{
GlobVarLog::set("CMR", "LOGS", GlobVarLog::get("CMR", "LOGS") - 1);
CMR_INFO("Log level set to %0") << GlobVarLog::get("CMR", "LOGS");
}
} else if (c == 'D')
{
if (GlobVarLog::get("CMR", "MYLOG") > 0)
{
GlobVarLog::set("CMR", "MYLOG", GlobVarLog::get("CMR", "MYLOG") - 1);
CMR_INFO("MYLOG Log level set to %0") << GlobVarLog::get("CMR", "MYLOG");
}
}
c = getchar();
}
CMR_INFO("Stop example Logs");
return r;
}