Commit ab5955c0 authored by lionelgo's avatar lionelgo

log options: stdout/rotate file (args options)

parent 8792d6c1
......@@ -97,6 +97,8 @@ install_spdlog_from_git() {
git clone $GIT_URL
cd spdlog && git checkout master
ret=$?;[[ $ret -ne 0 ]] && popd && return $ret
# enable syslog, Useless (defined in SPGW code)
sed -i '/#define SPDLOG_ENABLE_SYSLOG/s/^\/\///g' include/spdlog/tweakme.h
popd
fi
return 0
......
......@@ -15,6 +15,7 @@
*/
#include "logger.hpp"
#include "spdlog/sinks/syslog_sink.h"
#include <iostream>
#include <sstream>
......@@ -23,37 +24,29 @@
Logger *Logger::m_singleton = NULL;
void Logger::_init( const char *app )
void Logger::_init( const char *app, const bool log_stdout, bool const log_rot_file )
{
int num_sinks = 0;
spdlog::set_async_mode( 2048 );
#if TRACE_IS_ON
spdlog::level::level_enum llevel = spdlog::level::trace;
m_sinks.push_back( std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>() );
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::rotating_file_sink_mt>( filename, 5 * 1024 * 1024, 3 ) );
m_sinks[0].get()->set_level( llevel );
m_sinks[1].get()->set_level( llevel );
#elif DEBUG_IS_ON
spdlog::level::level_enum llevel = spdlog::level::debug;
m_sinks.push_back( std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>() );
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::rotating_file_sink_mt>( filename, 5 * 1024 * 1024, 3 ) );
m_sinks[0].get()->set_level( llevel );
m_sinks[1].get()->set_level( llevel );
#elif INFO_IS_ON
spdlog::level::level_enum llevel = spdlog::level::info;
m_sinks.push_back( std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>() );
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::rotating_file_sink_mt>( filename, 5 * 1024 * 1024, 3 ) );
m_sinks[0].get()->set_level( llevel );
m_sinks[1].get()->set_level( llevel );
#else
spdlog::level::level_enum llevel = spdlog::level::warn;
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::rotating_file_sink_mt>( filename, 5 * 1024 * 1024, 3 ) );
m_sinks[0].get()->set_level( llevel );
#endif
//m_sinks.push_back( std::make_shared<spdlog::sinks::syslog_sink>() );
if (log_stdout) {
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>() );
m_sinks[num_sinks++].get()->set_level( llevel );
}
if (log_rot_file) {
std::string filename = fmt::format("./{}.log", app);
m_sinks.push_back( std::make_shared<spdlog::sinks::rotating_file_sink_mt>( filename, 5 * 1024 * 1024, 3 ) );
m_sinks[num_sinks++].get()->set_level( llevel );
}
std::stringstream ss;
ss << "[%Y-%m-%dT%H:%M:%S.%f] [" << app << "] [%n] [%l] %v";
......
......@@ -74,8 +74,8 @@ class Logger
{
public:
static void init( const char *app ) { singleton()._init( app ); }
static void init( const std::string &app ) { init( app.c_str() ); }
static void init( const char *app, const bool log_stdout, const bool log_rot_file ) { singleton()._init( app, log_stdout, log_rot_file ); }
static void init( const std::string &app, const bool log_stdout, const bool log_rot_file ) { init( app.c_str(), log_stdout, log_rot_file ); }
static _Logger &async_cmd() { return *singleton().m_async_cmd; }
static _Logger &enb_s1u() { return *singleton().m_enb_s1u; }
......@@ -112,7 +112,7 @@ private:
Logger() {}
~Logger() {}
void _init( const char *app );
void _init( const char *app, const bool log_stdout, const bool log_rot_file);
std::vector<spdlog::sink_ptr> m_sinks;
......
......@@ -81,8 +81,6 @@ void my_app_signal_handler(int s)
int main(int argc, char **argv)
{
srand (time(NULL));
// Logger
Logger::init( "spgwc" );
// Command line options
if ( !Options::parse( argc, argv ) )
......@@ -90,6 +88,10 @@ int main(int argc, char **argv)
std::cout << "Options::parse() failed" << std::endl;
return 1;
}
// Logger
Logger::init( "spgwc" , Options::getlogStdout() , Options::getlogRotFilelog());
Logger::sgwc_app().startup( "Options parsed" );
struct sigaction sigIntHandler;
......
......@@ -25,7 +25,8 @@
int Options::options;
std::string Options::m_libconfigcfg;
bool Options::m_log_rot_file_log;
bool Options::m_log_stdout;
void Options::help()
{
......@@ -33,6 +34,8 @@ void Options::help()
<< "Usage: spgwc [OPTIONS]..." << std::endl
<< " -h, --help Print help and exit" << std::endl
<< " -c, --libconfigcfg filename Read the application configuration from this file." << std::endl
<< " -o, --stdoutlog Send the application logs to STDOUT fd." << std::endl
<< " -r, --rotatelog Send the application logs to local file (in current working directory)." << std::endl
;
}
......@@ -64,21 +67,24 @@ bool Options::parseInputOptions( int argc, char **argv )
struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "libconfigcfg", required_argument, NULL, 'f' },
{ "stdoutlog", no_argument, NULL, 'o' },
{ "rotatelog", no_argument, NULL, 'r' },
{ NULL,0,NULL,0 }
};
// Loop on arguments
while (1)
{
c = getopt_long(argc, argv, "hc:", long_options, &option_index );
c = getopt_long(argc, argv, "horc:", long_options, &option_index );
if (c == -1)
break; // Exit from the loop.
switch (c)
{
case 'h': { help(); exit(0); break; }
case 'c': { m_libconfigcfg = optarg; options |= libconfigcfg; break; }
case 'h': { help(); exit(0); break; }
case 'c': { m_libconfigcfg = optarg; options |= libconfigcfg; break; }
case 'o': { m_log_stdout = true; options |= log_stdout; break; }
case 'r': { m_log_rot_file_log = true; options |= log_rot_file_log; break; }
case '?':
......@@ -86,6 +92,8 @@ bool Options::parseInputOptions( int argc, char **argv )
switch ( optopt )
{
case 'c': { std::cout << "Option -l (libconfig config) requires an argument" << std::endl; break; }
case 'o': { std::cout << "Option -o do not requires an argument, can be also set with option -r." << std::endl; break; }
case 'r': { std::cout << "Option -r do not requires an argument, can be also set with option -o." << std::endl; break; }
default: { std::cout << "Unrecognized option [" << c << "]" << std::endl; break; }
}
result = false;
......
......@@ -30,18 +30,24 @@ public:
static bool parseJson();
static bool validateOptions();
static const std::string &getlibconfigConfig() { return m_libconfigcfg; }
static const std::string &getlibconfigConfig() { return m_libconfigcfg; }
static const bool &getlogRotFilelog() { return m_log_rot_file_log; }
static const bool &getlogStdout() { return m_log_stdout; }
private:
enum OptionsSelected {
libconfigcfg = 0x01,
libconfigcfg = 0x01,
log_stdout = 0x02,
log_rot_file_log = 0x04
};
static void help();
static int options;
static bool m_log_rot_file_log;
static bool m_log_stdout;
static std::string m_libconfigcfg;
};
......
......@@ -63,8 +63,6 @@ void my_app_signal_handler(int s){
//------------------------------------------------------------------------------
int main(int argc, char **argv)
{
// Logger
Logger::init( "spgwu");
// Command line options
if ( !Options::parse( argc, argv ) )
......@@ -72,6 +70,10 @@ int main(int argc, char **argv)
std::cout << "Options::parse() failed" << std::endl;
return 1;
}
// Logger
Logger::init( "spgwu" , Options::getlogStdout() , Options::getlogRotFilelog());
Logger::spgwu_app().startup( "Options parsed" );
struct sigaction sigIntHandler;
......
......@@ -25,7 +25,8 @@
int Options::options;
std::string Options::m_libconfigcfg;
bool Options::m_log_rot_file_log;
bool Options::m_log_stdout;
void Options::help()
{
......@@ -33,6 +34,8 @@ void Options::help()
<< "Usage: spgwu [OPTIONS]..." << std::endl
<< " -h, --help Print help and exit" << std::endl
<< " -c, --libconfigcfg filename Read the application configuration from this file." << std::endl
<< " -o, --stdoutlog Send the application logs to STDOUT fd." << std::endl
<< " -r, --rotatelog Send the application logs to local file (in current working directory)." << std::endl
;
}
......@@ -64,21 +67,24 @@ bool Options::parseInputOptions( int argc, char **argv )
struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "libconfigcfg", required_argument, NULL, 'f' },
{ "stdoutlog", no_argument, NULL, 'o' },
{ "rotatelog", no_argument, NULL, 'r' },
{ NULL,0,NULL,0 }
};
// Loop on arguments
while (1)
{
c = getopt_long(argc, argv, "hc:", long_options, &option_index );
c = getopt_long(argc, argv, "horc:", long_options, &option_index );
if (c == -1)
break; // Exit from the loop.
switch (c)
{
case 'h': { help(); exit(0); break; }
case 'c': { m_libconfigcfg = optarg; options |= libconfigcfg; break; }
case 'h': { help(); exit(0); break; }
case 'c': { m_libconfigcfg = optarg; options |= libconfigcfg; break; }
case 'o': { m_log_stdout = true; options |= log_stdout; break; }
case 'r': { m_log_rot_file_log = true; options |= log_rot_file_log; break; }
case '?':
......@@ -86,6 +92,8 @@ bool Options::parseInputOptions( int argc, char **argv )
switch ( optopt )
{
case 'c': { std::cout << "Option -l (libconfig config) requires an argument" << std::endl; break; }
case 'o': { std::cout << "Option -o do not requires an argument, can be also set with option -r." << std::endl; break; }
case 'r': { std::cout << "Option -r do not requires an argument, can be also set with option -o." << std::endl; break; }
default: { std::cout << "Unrecognized option [" << c << "]" << std::endl; break; }
}
result = false;
......
......@@ -30,18 +30,24 @@ public:
static bool parseJson();
static bool validateOptions();
static const std::string &getlibconfigConfig() { return m_libconfigcfg; }
static const std::string &getlibconfigConfig() { return m_libconfigcfg; }
static const bool &getlogRotFilelog() { return m_log_rot_file_log; }
static const bool &getlogStdout() { return m_log_stdout; }
private:
enum OptionsSelected {
libconfigcfg = 0x01,
libconfigcfg = 0x01,
log_stdout = 0x02,
log_rot_file_log = 0x04
};
static void help();
static int options;
static bool m_log_rot_file_log;
static bool m_log_stdout;
static std::string m_libconfigcfg;
};
......
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