11 #ifndef logger_logManager_hpp    12 #define logger_logManager_hpp    67 template<
class logFileT>
    77    bool m_logShutdown {
false}; 
    83    int m_logThreadPrio {0};
    85    bool m_logThreadRunning {
false};
   100    int writePause( 
const unsigned long & wp );
   105    unsigned long writePause();
   130    int logThreadPrio( 
int newPrio );
   140    bool logThreadRunning();
   143    int setupConfig( mx::appConfigurator & config );
   148    int loadConfig( mx::appConfigurator & config );
   152    static void _logThreadStart( 
logManager * l );
   155    int logThreadStart();
   158    void logThreadExec();
   167    template<
typename logT>
   169                          const typename logT::messageT & msg, 
   180    template<
typename logT>
   183                          const typename logT::messageT & msg, 
   191    template<
typename logT>
   192    void log( 
const typename logT::messageT & msg, 
   200    template<
typename logT>
   202              const typename logT::messageT & msg, 
   210    template<
typename logT>
   217    template<
typename logT>
   224 template<
class logFileT>
   229 template<
class logFileT>
   232    m_logShutdown = 
true;
   234    if(m_logThread.joinable()) m_logThread.join();
   237    if( !m_logQueue.empty() ) logThreadExec();
   241 template<
class logFileT>
   244    if(wp == 0) 
return -1;
   251 template<
class logFileT>
   257 template<
class logFileT>
   267 template<
class logFileT>
   273 template<
class logFileT>
   276    if(newPrio > 98) 
return -1; 
   277    if(newPrio < 0) newPrio = 0;
   279    m_logThreadPrio = newPrio;
   283 template<
class logFileT>
   286    return m_logThreadPrio;
   289 template<
class logFileT>
   292    return m_logThreadRunning;
   295 template<
class logFileT>
   298    config.add(
"logger.logDir",
"L", 
"logDir",mx::argType::Required, 
"logger", 
"logDir", 
false, 
"string", 
"The directory for log files");
   299    config.add(
"logger.logExt",
"", 
"logExt",mx::argType::Required, 
"logger", 
"logExt", 
false, 
"string", 
"The extension for log files");
   300    config.add(
"logger.maxLogSize",
"", 
"maxLogSize",mx::argType::Required, 
"logger", 
"maxLogSize", 
false, 
"string", 
"The maximum size of log files");
   301    config.add(
"logger.writePause",
"", 
"writePause",mx::argType::Required, 
"logger", 
"writePause", 
false, 
"unsigned long", 
"The log thread pause time in ns");
   302    config.add(
"loger.logThreadPrio", 
"", 
"logThreadPrio", mx::argType::Required, 
"logger", 
"logThreadPrio", 
false, 
"int", 
"The log thread priority");
   303    config.add(
"logger.logLevel",
"l", 
"logLevel",mx::argType::Required, 
"logger", 
"logLevel", 
false, 
"string", 
"The log level");
   308 template<
class logFileT>
   313    config(tmp, 
"logger.logDir");
   314    if(tmp != 
"") this->logPath(tmp);
   318    config(tmp, 
"logger.logLevel");
   328          std::cerr << 
"Unkown log level specified.  Using default (INFO)\n";
   336    config(this->m_logExt, 
"logger.logExt");
   339    config(this->m_maxLogSize, 
"logger.maxLogSize");
   342    config(m_writePause, 
"logger.writePause");
   345    config(m_logThreadPrio, 
"logger.logThreadPrio");
   350 template<
class logFileT>
   356 template<
class logFileT>
   361       m_logThread = std::thread( _logThreadStart, 
this);
   363    catch( 
const std::exception & e )
   365       log<software_error>({__FILE__,__LINE__, 0, 0, std::string(
"Exception on log thread start: ") + e.what()});
   370       log<software_error>({__FILE__,__LINE__, 0, 0, 
"Unkown exception on log thread start"});
   374    if(!m_logThread.joinable())
   376       log<software_error>({__FILE__, __LINE__, 0, 0,  
"Log thread did not start"});
   382    sp.sched_priority = m_logThreadPrio;
   384    int rv = pthread_setschedparam( m_logThread.native_handle(), SCHED_OTHER, &sp);
   388       log<software_error>({__FILE__, __LINE__, 0, rv, std::string(
"Error setting thread params: ") + strerror(rv)});
   396 template<
class logFileT>
   400    m_logThreadRunning = 
true;
   402    std::unique_lock<std::mutex> lock(m_qMutex, std::defer_lock);
   404    while(!m_logShutdown || !m_logQueue.empty())
   406       std::list<bufferPtrT>::iterator beg, it, er, end;
   411       beg = m_logQueue.begin();
   412       end = m_logQueue.end();
   422             if( this->writeLog( *it ) < 0) 
   424                m_logThreadRunning = 
false;
   433             m_logQueue.erase(er);
   442       if(m_logQueue.empty() && !m_logShutdown) std::this_thread::sleep_for( std::chrono::duration<unsigned long, std::nano>(m_writePause));
   445    m_logThreadRunning = 
false;
   448 template<
class logFileT>
   449 template<
typename logT>
   451                                      const typename logT::messageT & msg,
   459    return logHeader::createLog<logT>(logBuffer, ts, msg, level);
   462 template<
class logFileT>
   463 template<
typename logT>
   466                                      const typename logT::messageT & msg,
   470    return logHeader::createLog<logT>(logBuffer, ts, msg, level);
   473 template<
class logFileT>
   474 template<
typename logT>
   482    if(level > m_logLevel) 
return; 
   486    createLog<logT>(logBuffer, msg, level);
   489    std::lock_guard<std::mutex> guard(m_qMutex);  
   490    m_logQueue.push_back(logBuffer);
   495 template<
class logFileT>
   496 template<
typename logT>
   498                                 const typename logT::messageT & msg,
   505    if(level > m_logLevel) 
return; 
   509    createLog<logT>(logBuffer, ts, msg, level);
   512    std::lock_guard<std::mutex> guard(m_qMutex);  
   513    m_logQueue.push_back(logBuffer);
   517 template<
class logFileT>
   518 template<
typename logT>
   524 template<
class logFileT>
   525 template<
typename logT>
   537 #endif //logger_logger_hpp 
std::thread m_logThread
A separate thread for actually writing to the file. 
 
static constexpr logPrioT LOG_INFO
Informational. The info log level is the lowest level recorded during normal operations. 
 
The standard MagAOX log manager, used for both process logs and telemetry streams. 
 
std::list< bufferPtrT > m_logQueue
Log entries are stored here, and writen to the file by the log thread. 
 
#define MAGAOX_default_writePause
The default logger writePause. 
 
logPrioT logLevelFromString(const std::string &str)
Get the log priority from a string, which might have the number or the name. 
 
Empty type for resolving logs with no message. 
 
static constexpr logPrioT LOG_UNKNOWN
Used to denote an unkown log type for internal error handling. 
 
int8_t logPrioT
The type of the log priority code. 
 
void logThreadExec()
Execute the logger thread. 
 
void gettime()
Fill the the timespecX with the current time. 
 
std::shared_ptr< char > bufferPtrT
The log entry buffer smart pointer. 
 
std::mutex m_qMutex
Mutex for accessing the m_logQueue. 
 
Flatlogs single include file. 
 
static constexpr logPrioT LOG_DEFAULT
Used to denote "use the default level for this log type". 
 
A fixed-width timespec structure.