C + + logging module

Source: Internet
Author: User

C + + logging module

The module is generated from the actual project, through the way of extern declaration, you can generate a log in different modules of code, the log file name is called random code plus user-specified name, the use of random code is to avoid the problem of the log file may be overwritten.

If you want to, you can build your own personal logging module, this sharing module implementation method is relatively simple, there may be some places did not consider clearly.

Source:
 ////Created by Jerry on 2/12/16.//#include <iostream> #include <string> #include    <fstream> #include <sys/time.h> #include <unistd.h> #include <stdlib.h>namespace lg{class Log {        Private:std::string M_log_file_path;        Std::ofstream M_log_file;    BOOL M_cout_flag;        Public:log (const std::string file_path = "Run.log", const bool Cout_flag = TRUE);        ~log ();        void Close (); Template<class t> Log & operator << (T &log_data) {m_log_file << log_d            Ata M_log_file << Std::flush; #if ((Defined _rm_dec_print) && (defined _rm_dec_log_print)) if (M_cout_f            LAG) std::cout << log_data << Std::flush;        #endif return *this;    }    }; extern Log Run_log;}  
Created by Jerry on 2/12/16.//#include "Log.h" namespace lg{Log Run_log;        Log::log (const std::string file_path, const bool cout_flag) {m_cout_flag = Cout_flag;                System ("mkdir $HOME/data/log");             struct Timeval TV;        Gettimeofday (&tv,null);          Std::random_device Rd;          std::d efault_random_engine E (rd ());        std::uniform_int_distribution<> u (0,1000000);                Usleep (U (e));                 std::string home = getenv ("Home"); std::string Log_file_path = home + "/data/log/" + std::to_string ((tv.tv_sec * 1000)% 10000        + tv.tv_usec/1000) + std::to_string (U (e)) + File_path;        M_log_file_path = Log_file_path;    M_log_file.open (M_log_file_path, Std::ios::app);        } log::~log () {m_log_file.close ();    Std::cout << "--Log destructor successfully!" << Std::endl; }    void Log::close () {m_log_file.close (); }}
Routines:

The following log_information are log information that users need to log.

//file test1.cpp#include "Log.h"//...   your code here//...   your code herelg::run_log << /***log_information1 here***/ << "\n";
//file test2.cpp#include "Log.h"//...   your code here//...   your code herelg::run_log << /***log_information2 here***/ << "\n";

The final log output is:

#file (rand_code)run.loglog_information1log_information2

C + + logging module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.