libreoffice-online/common/Log.cpp
Michael Meeks 685709080d Revert attempts at re-using ostringstream
Cleaning up the thread variable with the shared string stream is
    something of a nightmare, for a rather marginal gain.

    ==9296== Invalid write of size 1
    ...
    ==9296==    by 0x738C092: str (sstream:195)
    ==9296==    by 0x738C092: std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::str(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (sstream:649)
    ==9296==    by 0x65383A: Log::beginLog[abi:cxx11](char const*) (Log.cpp:141)
    ==9296==    by 0x551823: Admin::~Admin() (Admin.cpp:381)
    ==9296==    by 0x7D9ECF7: __run_exit_handlers (exit.c:83)
    ==9296==    by 0x7D9ED49: exit (exit.c:105)
    ==9296==    by 0x7D86F50: (below main) (libc-start.c:342)
    ==9296==  Address 0x8ba41c0 is 0 bytes inside a block of size 513 free'd
    ==9296==    at 0x4C2FA1D: operator delete(void*) (vg_replace_malloc.c:576)
    ...
    ==9296==    by 0x738784A: ~basic_stringbuf (sstream:65)
    ==9296==    by 0x738784A: std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_ostringstream() (sstream:591)
    ==9296==    by 0x7D9F27E: __call_tls_dtors (cxa_thread_atexit_impl.c:155)
    ==9296==    by 0x7D9EC0A: __run_exit_handlers (exit.c:41)
    ==9296==    by 0x7D9ED49: exit (exit.c:105)
    ==9296==    by 0x7D86F50: (below main) (libc-start.c:342)

    Good to log during shutdown / exit.

This reverts commit c315d219d5967f23fb1769e78021f61b8f9da6ec.
This reverts commit ce78fec310.

Change-Id: Ia4a15be336d89d8d883530943724d48e4b0ec9fe
Reviewed-on: https://gerrit.libreoffice.org/71444
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Tested-by: Michael Meeks <michael.meeks@collabora.com>
2019-04-27 23:18:11 +02:00

211 lines
5.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#ifdef __linux
#include <sys/prctl.h>
#include <sys/syscall.h>
#endif
#include <unistd.h>
#include <atomic>
#include <cassert>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <Poco/ConsoleChannel.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/FileChannel.h>
#include <Poco/FormattingChannel.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Process.h>
#include <Poco/SplitterChannel.h>
#include <Poco/Thread.h>
#include <Poco/Timestamp.h>
#include "Log.hpp"
#include "Util.hpp"
namespace Log
{
using namespace Poco;
/// Helper to avoid destruction ordering issues.
struct StaticNameHelper
{
private:
std::atomic<bool> _inited;
std::string _name;
std::string _id;
public:
StaticNameHelper() :
_inited(true)
{
}
~StaticNameHelper()
{
_inited = false;
}
bool getInited() const { return _inited; }
void setId(const std::string& id) { _id = id; }
const std::string& getId() const { return _id; }
void setName(const std::string& name) { _name = name; }
const std::string& getName() const { return _name; }
};
static StaticNameHelper Source;
// We need a signal safe means of writing messages
// $ man 7 signal
void signalLog(const char *message)
{
while (true)
{
const int length = std::strlen(message);
const int written = write (STDERR_FILENO, message, length);
if (written < 0)
{
if (errno == EINTR)
continue; // ignore.
else
break;
}
message += written;
if (message[0] == '\0')
break;
}
}
// We need a signal safe means of writing messages
// $ man 7 signal
void signalLogNumber(std::size_t num)
{
int i;
char buf[22];
buf[21] = '\0';
for (i = 20; i > 0 && num > 0; --i)
{
buf[i] = '0' + num % 10;
num /= 10;
}
signalLog(buf + i + 1);
}
char* prefix(char* buffer, const std::size_t len, const char* level)
{
const char *threadName = Util::getThreadName();
#ifdef __linux
const long osTid = Util::getThreadId();
#elif defined IOS
const auto osTid = pthread_mach_thread_np(pthread_self());
#endif
Poco::DateTime time;
snprintf(buffer, len, "%s-%.05lu %.4u-%.2u-%.2u %.2u:%.2u:%.2u.%.6u [ %s ] %s ",
(Source.getInited() ? Source.getId().c_str() : "<shutdown>"),
osTid,
time.year(), time.month(), time.day(),
time.hour(), time.minute(), time.second(),
time.millisecond() * 1000 + time.microsecond(),
threadName, level);
return buffer;
}
void signalLogPrefix()
{
char buffer[1024];
prefix(buffer, sizeof(buffer) - 1, "SIG");
signalLog(buffer);
}
void initialize(const std::string& name,
const std::string& logLevel,
const bool withColor,
const bool logToFile,
const std::map<std::string, std::string>& config)
{
Source.setName(name);
std::ostringstream oss;
oss << Source.getName();
#if !MOBILEAPP // Just one process in a mobile app, the pid is uninteresting.
oss << '-'
<< std::setw(5) << std::setfill('0') << Poco::Process::id();
#endif
Source.setId(oss.str());
// Configure the logger.
AutoPtr<Channel> channel;
if (logToFile)
{
channel = static_cast<Poco::Channel*>(new FileChannel("loolwsd.log"));
for (const auto& pair : config)
{
channel->setProperty(pair.first, pair.second);
}
}
else if (withColor)
channel = static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel());
else
channel = static_cast<Poco::Channel*>(new Poco::ConsoleChannel());
/**
* Open the channel explicitly, instead of waiting for first log message
* This is important especially for the kit process where opening the channel
* after chroot can cause file creation inside the jail instead of outside
* */
channel->open();
auto& logger = Poco::Logger::create(Source.getName(), channel, Poco::Message::PRIO_TRACE);
logger.setLevel(logLevel.empty() ? std::string("trace") : logLevel);
const std::time_t t = std::time(nullptr);
oss.str("");
oss.clear();
oss << "Initializing " << name << ".";
// TODO: replace with std::put_time when we move to gcc 5+.
char buf[32];
if (strftime(buf, sizeof(buf), "%a %F %T%z", std::localtime(&t)) > 0)
{
oss << " Local time: " << buf << ".";
}
oss << " Log level is [" << logger.getLevel() << "].";
LOG_INF(oss.str());
}
Poco::Logger& logger()
{
return Poco::Logger::get(Source.getInited() ? Source.getName() : std::string());
}
void shutdown()
{
logger().shutdown();
// Flush
std::flush(std::cout);
fflush(stdout);
std::flush(std::cerr);
fflush(stderr);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */