2016-04-14 19:12:43 -05:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-03-08 10:38:22 -06:00
|
|
|
|
2018-08-29 10:47:05 -05:00
|
|
|
#ifdef __linux
|
2016-04-14 19:12:43 -05:00
|
|
|
#include <sys/prctl.h>
|
2017-01-19 18:26:57 -06:00
|
|
|
#include <sys/syscall.h>
|
2018-08-29 10:47:05 -05:00
|
|
|
#endif
|
2017-01-19 18:26:57 -06:00
|
|
|
#include <unistd.h>
|
2016-04-14 19:12:43 -05:00
|
|
|
|
2016-08-07 22:32:39 -05:00
|
|
|
#include <atomic>
|
2016-04-14 19:12:43 -05:00
|
|
|
#include <cassert>
|
2017-01-08 11:21:14 -06:00
|
|
|
#include <ctime>
|
2016-04-14 19:12:43 -05:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <Poco/ConsoleChannel.h>
|
2017-01-13 09:09:47 -06:00
|
|
|
#include <Poco/DateTimeFormatter.h>
|
2016-08-07 22:32:39 -05:00
|
|
|
#include <Poco/FileChannel.h>
|
|
|
|
#include <Poco/FormattingChannel.h>
|
|
|
|
#include <Poco/PatternFormatter.h>
|
2016-04-14 19:12:43 -05:00
|
|
|
#include <Poco/Process.h>
|
2016-08-07 22:32:39 -05:00
|
|
|
#include <Poco/SplitterChannel.h>
|
2016-04-14 19:12:43 -05:00
|
|
|
#include <Poco/Thread.h>
|
2016-07-30 22:06:12 -05:00
|
|
|
#include <Poco/Timestamp.h>
|
2016-04-14 19:12:43 -05:00
|
|
|
|
|
|
|
#include "Log.hpp"
|
2017-03-30 12:14:40 -05:00
|
|
|
#include "Util.hpp"
|
2016-04-14 19:12:43 -05:00
|
|
|
|
|
|
|
namespace Log
|
|
|
|
{
|
2016-08-07 22:32:39 -05:00
|
|
|
using namespace Poco;
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Helper to avoid destruction ordering issues.
|
2018-09-13 03:52:08 -05:00
|
|
|
struct StaticNameHelper
|
2016-10-30 18:07:41 -05:00
|
|
|
{
|
2016-08-07 22:32:39 -05:00
|
|
|
std::atomic<bool> inited;
|
2016-04-14 19:12:43 -05:00
|
|
|
std::string name;
|
|
|
|
std::string id;
|
2018-09-13 03:52:08 -05:00
|
|
|
StaticNameHelper() :
|
2016-04-14 19:12:43 -05:00
|
|
|
inited(true)
|
|
|
|
{
|
|
|
|
}
|
2018-09-13 03:52:08 -05:00
|
|
|
~StaticNameHelper()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
|
|
|
inited = false;
|
|
|
|
}
|
|
|
|
};
|
2018-09-13 03:52:08 -05:00
|
|
|
static StaticNameHelper Source;
|
2016-04-14 19:12:43 -05:00
|
|
|
|
2016-04-15 14:46:44 -05:00
|
|
|
// We need a signal safe means of writing messages
|
|
|
|
// $ man 7 signal
|
|
|
|
void signalLog(const char *message)
|
|
|
|
{
|
2016-10-30 18:07:41 -05:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const int length = strlen(message);
|
|
|
|
const int written = write (STDERR_FILENO, message, length);
|
2016-04-15 14:46:44 -05:00
|
|
|
if (written < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue; // ignore.
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2016-10-30 18:07:41 -05:00
|
|
|
|
2016-04-15 14:46:44 -05:00
|
|
|
message += written;
|
|
|
|
if (message[0] == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:05:45 -05:00
|
|
|
// We need a signal safe means of writing messages
|
|
|
|
// $ man 7 signal
|
|
|
|
void signalLogNumber(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);
|
|
|
|
}
|
|
|
|
|
2017-03-30 12:14:40 -05:00
|
|
|
char* prefix(char* buffer, const char* level, bool sigSafe)
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2018-08-29 10:47:05 -05:00
|
|
|
#ifdef __linux
|
2017-03-30 12:14:40 -05:00
|
|
|
long osTid;
|
2017-01-19 18:26:57 -06:00
|
|
|
char procName[32];
|
2017-03-30 12:14:40 -05:00
|
|
|
const char *threadName = procName;
|
|
|
|
if (sigSafe)
|
2016-10-30 18:07:41 -05:00
|
|
|
{
|
2017-03-30 12:14:40 -05:00
|
|
|
osTid = syscall(SYS_gettid);
|
|
|
|
|
|
|
|
if (prctl(PR_GET_NAME, reinterpret_cast<unsigned long>(procName), 0, 0, 0) != 0)
|
|
|
|
strncpy(procName, "<noid>", sizeof(procName) - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osTid = Util::getThreadId();
|
|
|
|
threadName = Util::getThreadName();
|
2016-10-30 18:07:41 -05:00
|
|
|
}
|
2018-09-13 11:16:00 -05:00
|
|
|
#elif defined IOS
|
|
|
|
const char *threadName = Util::getThreadName();
|
|
|
|
auto osTid = pthread_mach_thread_np(pthread_self());
|
|
|
|
#endif
|
2017-01-17 17:30:58 -06:00
|
|
|
Poco::DateTime time;
|
2018-04-19 07:35:23 -05:00
|
|
|
snprintf(buffer, 1023, "%s-%.05lu %.4u-%.2u-%.2u %.2u:%.2u:%.2u.%.6u [ %s ] %s ",
|
2017-01-19 18:26:57 -06:00
|
|
|
(Source.inited ? Source.id.c_str() : "<shutdown>"),
|
|
|
|
osTid,
|
2018-04-19 07:35:23 -05:00
|
|
|
time.year(), time.month(), time.day(),
|
2017-01-19 18:26:57 -06:00
|
|
|
time.hour(), time.minute(), time.second(),
|
|
|
|
time.millisecond() * 1000 + time.microsecond(),
|
2017-03-30 12:14:40 -05:00
|
|
|
threadName, level);
|
2017-01-19 18:26:57 -06:00
|
|
|
return buffer;
|
2016-04-15 14:46:44 -05:00
|
|
|
}
|
2016-04-14 19:12:43 -05:00
|
|
|
|
2016-04-15 14:46:44 -05:00
|
|
|
void signalLogPrefix()
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
2017-03-30 12:14:40 -05:00
|
|
|
prefix(buffer, "SIG", true);
|
2016-04-15 14:46:44 -05:00
|
|
|
signalLog(buffer);
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:32:39 -05:00
|
|
|
void initialize(const std::string& name,
|
|
|
|
const std::string& logLevel,
|
|
|
|
const bool withColor,
|
|
|
|
const bool logToFile,
|
2018-08-13 02:26:09 -05:00
|
|
|
const std::map<std::string, std::string>& config)
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
|
|
|
Source.name = name;
|
|
|
|
std::ostringstream oss;
|
2018-09-13 11:16:00 -05:00
|
|
|
oss << Source.name;
|
|
|
|
#ifndef MOBILEAPP // Just one process in a mobile app, the pid is uninteresting.
|
|
|
|
oss << '-'
|
2016-04-14 19:12:43 -05:00
|
|
|
<< std::setw(5) << std::setfill('0') << Poco::Process::id();
|
2018-09-13 11:16:00 -05:00
|
|
|
#endif
|
2016-04-14 19:12:43 -05:00
|
|
|
Source.id = oss.str();
|
|
|
|
|
2016-07-30 10:52:10 -05:00
|
|
|
// Configure the logger.
|
2016-09-27 07:30:45 -05:00
|
|
|
AutoPtr<Channel> channel;
|
2016-08-07 22:32:39 -05:00
|
|
|
|
|
|
|
if (logToFile)
|
|
|
|
{
|
2016-09-27 07:30:45 -05:00
|
|
|
channel = static_cast<Poco::Channel*>(new FileChannel("loolwsd.log"));
|
2016-08-07 22:32:39 -05:00
|
|
|
for (const auto& pair : config)
|
|
|
|
{
|
2016-09-27 07:30:45 -05:00
|
|
|
channel->setProperty(pair.first, pair.second);
|
2016-08-07 22:32:39 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-27 07:30:45 -05:00
|
|
|
else if (withColor)
|
|
|
|
channel = static_cast<Poco::Channel*>(new Poco::ColorConsoleChannel());
|
|
|
|
else
|
|
|
|
channel = static_cast<Poco::Channel*>(new Poco::ConsoleChannel());
|
2016-08-07 22:32:39 -05:00
|
|
|
|
2017-07-12 04:38:59 -05:00
|
|
|
/**
|
|
|
|
* 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();
|
2016-04-14 19:12:43 -05:00
|
|
|
auto& logger = Poco::Logger::create(Source.name, channel, Poco::Message::PRIO_TRACE);
|
|
|
|
|
2016-07-30 10:52:10 -05:00
|
|
|
logger.setLevel(logLevel.empty() ? std::string("trace") : logLevel);
|
2016-04-14 19:12:43 -05:00
|
|
|
|
2017-01-08 11:21:14 -06:00
|
|
|
const std::time_t t = std::time(nullptr);
|
|
|
|
oss.str("");
|
|
|
|
oss.clear();
|
2017-01-09 11:33:01 -06:00
|
|
|
|
|
|
|
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() << "].";
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF(oss.str());
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Logger& logger()
|
|
|
|
{
|
|
|
|
return Poco::Logger::get(Source.inited ? Source.name : std::string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|