dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 2 | * Copyright (C) 2018 sysmocom - s.f.m.c. GmbH |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 5 | * |
| 6 | * This software is distributed under the terms of the GNU Affero Public License. |
| 7 | * See the COPYING file in the main directory for details. |
| 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
| 11 | |
| 12 | This program is free software: you can redistribute it and/or modify |
| 13 | it under the terms of the GNU Affero General Public License as published by |
| 14 | the Free Software Foundation, either version 3 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU Affero General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU Affero General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | */ |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <cstdio> |
| 29 | #include <fstream> |
| 30 | #include <string> |
kurtis.heimerl | 00913d7 | 2012-12-16 06:08:18 +0000 | [diff] [blame] | 31 | #include <stdarg.h> |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 32 | #include <sys/time.h> // For gettimeofday |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 33 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 34 | #include "Logger.h" |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 35 | #include "Threads.h" // pat added |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 36 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 37 | using namespace std; |
| 38 | |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 39 | std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) |
| 40 | { |
| 41 | return os << ss.str(); |
| 42 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 43 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 44 | Log::~Log() |
| 45 | { |
Pau Espin Pedrol | 86be40b | 2018-09-03 15:22:50 +0200 | [diff] [blame] | 46 | int old_state; |
Pau Espin Pedrol | 61837c0 | 2018-02-20 18:22:18 +0100 | [diff] [blame] | 47 | int mlen = mStream.str().size(); |
| 48 | int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 49 | const char *fmt = neednl ? "%s\n" : "%s"; |
Pau Espin Pedrol | c9202ab | 2019-07-01 20:13:32 +0200 | [diff] [blame] | 50 | |
Pau Espin Pedrol | b7e9927 | 2019-09-17 20:26:53 +0200 | [diff] [blame] | 51 | /* print related function called inside a C++ destructor, use pthread_setcancelstate() APIs. |
| 52 | See osmo-trx commit 86be40b4eb762d5c12e8e3f7388ca9f254e77b36 for more information */ |
| 53 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state); |
Pau Espin Pedrol | 3b78cbf | 2018-04-25 16:43:02 +0200 | [diff] [blame] | 54 | LOGPSRC(mCategory, mPriority, filename, line, fmt, mStream.str().c_str()); |
Pau Espin Pedrol | b7e9927 | 2019-09-17 20:26:53 +0200 | [diff] [blame] | 55 | pthread_setcancelstate(old_state, NULL); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 56 | } |
| 57 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 58 | ostringstream& Log::get() |
| 59 | { |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 60 | return mStream; |
| 61 | } |
| 62 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 63 | // vim: ts=4 sw=4 |