blob: 171c6350a841367461625dc54cc498088434b3be [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +01002* Copyright (C) 2018 sysmocom - s.f.m.c. GmbH
dburgess82c46ff2011-10-07 02:40:51 +00003*
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26#include <string.h>
27#include <cstdio>
28#include <fstream>
29#include <string>
kurtis.heimerl00913d72012-12-16 06:08:18 +000030#include <stdarg.h>
Alexander Chemeris4793f462017-03-17 18:35:48 -070031#include <sys/time.h> // For gettimeofday
dburgess82c46ff2011-10-07 02:40:51 +000032
dburgess82c46ff2011-10-07 02:40:51 +000033#include "Logger.h"
kurtis.heimerl5a872472013-05-31 21:47:25 +000034#include "Threads.h" // pat added
dburgess82c46ff2011-10-07 02:40:51 +000035
dburgess82c46ff2011-10-07 02:40:51 +000036using namespace std;
37
Alexander Chemerisa8cf2082015-07-30 20:04:18 -040038Mutex gLogToLock;
39
Alexander Chemeris4793f462017-03-17 18:35:48 -070040std::ostream& operator<<(std::ostream& os, std::ostringstream& ss)
41{
42 return os << ss.str();
43}
dburgess82c46ff2011-10-07 02:40:51 +000044
dburgess82c46ff2011-10-07 02:40:51 +000045Log::~Log()
46{
Pau Espin Pedrol86be40b2018-09-03 15:22:50 +020047 int old_state;
48 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
Pau Espin Pedrol61837c02018-02-20 18:22:18 +010049 int mlen = mStream.str().size();
50 int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n');
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010051 const char *fmt = neednl ? "%s\n" : "%s";
Pau Espin Pedrol61837c02018-02-20 18:22:18 +010052 ScopedLock lock(gLogToLock);
53 // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers,
54 // so just use std::cout.
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020055 LOGPSRC(mCategory, mPriority, filename, line, fmt, mStream.str().c_str());
Pau Espin Pedrol86be40b2018-09-03 15:22:50 +020056 pthread_setcancelstate(old_state, NULL);
dburgess82c46ff2011-10-07 02:40:51 +000057}
58
dburgess82c46ff2011-10-07 02:40:51 +000059ostringstream& Log::get()
60{
dburgess82c46ff2011-10-07 02:40:51 +000061 return mStream;
62}
63
dburgess82c46ff2011-10-07 02:40:51 +000064// vim: ts=4 sw=4