[[email protected] tmp]# cat logger.h #ifndef LOGGER_UTIL_INCLUDED#define logger_util_included/* copyright (c) 2015, Oracle and/or its affiliates. all rights reserved. this program is free Software; you can redistribute it and/or modify it under the terms of the gnu general public license as published by the free software foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or Fitness for a particular purpose. see the gnu general public license For more details. you should have received a copy of the GNU General Public License along with this Program; if not, write to the free software foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 usa*//** a trivial placeholder for inserting time signatures into streams. @example cout << datetime () << "[Info] today was a sunny day " << endl;*/#include <ostream># Include <string> #include <sstream>using namespace std;struct datetime {};ostream &operator<< (OSTREAM&NBSP;&OS,&NBSP;CONST&NBSP;DATETIME&NBSP;&DT); Class gen_spaces{public: gen _spaces (Int s) { m_spaces.assign (S, ' '); } ostream &operator<< (Ostream &os) { return os; } friend ostream &operator<< (ostream &os, const Gen_spaces &gen);p Rivate: string m_spaces;};o stream &operator<< (Ostream &os, const gen_spaces &gen);class Log : public ostream{public: log (Ostream &str, string logclass) : ostream (NULL), m_buffer (Str, logclass) { This->init (&m_buffer); } void enabled (bool s) { m_ Buffer.enabled (s); }private: class log_buff&nbSp;: public stringbuf { public: log_buff (ostream &STR,&NBSP;STRING&NBSP;&LOGC) :m_os (str), M_LOGC (LOGC), m_ Enabled (True) {} void set_log_class (string &s) { m_logc= s; } void enabled (bool s) { m_ Enabled= s; } virtual int sync (); private: ostream &m_os; string m_logc; bool m_enabled; }; Log_buff m_buffer;}; #endif /* logger_util_included */[[email protected] tmp]# cat logger.cc /* Copyright (c) 2015, oracle and/or its affiliates. all rights reserved. this program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as Published by the free software foundation; version 2 of the license. this program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of merchantability or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public license for more details. you should have received a copy of the GNU General Public License along with this program; if not, write to the free software foundation, inc., 51 franklin st, fifth floor, boston, ma 02110-1301 usa */#include "logger.h" #include <iostream> #include < locale>ostream &operator<< (OSTREAM&NBSP;&OS,&NBSP;CONST&NBSP;DATETIME&NBSP;&DT) { const char format[]= "%y-%m-%d %x"; Time_t t (Time (NULL)); Tm tm (*localtime (&t) ); Std::locale loc (Cout.getloc ());ostringstream sout;const std::time_put<char> & Tput =std::use_facet<std::time_put<char> > (Loc); Tput.put (Sout.rdbuf (), sout, ' ( &tm, &format[0], &format[11]); Os << sout.str () << " "; return os;} ostream &operator<< (Ostream &os, const gen_spaces &gen) {return Os << gen.m_spaces;} Int log::log_buff::sync () {string sout (str ());if (M_enabled && sout.length () > 0) {m_os << datetime () << "[" << m_logc << "]" << gen_spaces (8-m_logc.length ()) << sout;} STR (""); M_os.flush (); return 0;} [[email protected] tmp]# cat main.cpp #include <iostream> #include " Logger.h "Using namespace std;int main (void) {cout << datetime () << "[Test] today's weather is good!] << endl; return 0;} [[Email protected] tmp]# g++ main.cpp logger.cc && ./a.out 2016-11-03 17:44:09 [Test] Today's weather is good! [[email protected] tmp]#
This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1869084
Logs extracted from MySQL