1
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
3
* Copyright 2012 Canonical Ltd.
5
* This program is free software: you can redistribute it and/or modify it
6
* under the terms of the GNU Lesser General Public License, as
7
* published by the Free Software Foundation; either version 2.1 or 3.0
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranties of
12
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
13
* PURPOSE. See the applicable version of the GNU Lesser General Public
14
* License for more details.
16
* You should have received a copy of both the GNU Lesser General Public
17
* License along with this program. If not, see <http://www.gnu.org/licenses/>
19
* Authored by: Thomas Voß <thomas.voss@canonical.com>
22
#include "mario/logging/configuration.h"
23
#include "mario/logging/logging.h"
24
#include "mario/logging/asynchronous_configuration.h"
25
#include "mario/logging/synchronous_configuration.h"
26
#include "mario/logging/writer.h"
27
#include "mario/logging/backend/async_backend.h"
28
#include "mario/logging/backend/output_stream_backend.h"
29
#include "mario/logging/backend/syslog_backend.h"
31
#include "environment.h"
33
#include <gmock/gmock.h>
34
#include <gtest/gtest.h>
36
#include <boost/asio/io_service.hpp>
37
#include <boost/bind.hpp>
38
#include <boost/thread.hpp>
40
namespace ml = mario::logging;
41
namespace mlt = mario::logging::testing;
47
static const int do_overwrite = 1;
50
ml::Configuration::default_log_level_env().c_str(),
55
struct MockConfiguration : public ml::Configuration
57
MockConfiguration() {}
59
MOCK_METHOD0(create_default_writer, ml::Writer*());
60
MOCK_METHOD0(create_syslog_writer, ml::Writer*());
67
a_configuration_can_be_initialized_from_environment_and_from_command_line)
71
std::shared_ptr<ml::Configuration> config = ml::default_synchronous_configuration();
73
ml::Configuration::updated,
74
config->update_from_environment());
77
config->default_log_level());
79
ml::Configuration::updated,
80
config->update_from_command_line(mlt::Environment::argc(), mlt::Environment::argv()));
83
config->default_log_level());
85
mlt::Environment::argv()[0],
86
config->executable_name());
95
void operator()(T*) const
102
LoggingInitialization,
103
initializing_the_library_calls_create_loggers)
105
using namespace ::testing;
107
MockConfiguration mock;
108
std::shared_ptr<ml::Configuration> config(&mock, NullDeleter());
109
EXPECT_CALL(mock, create_default_writer()).Times(AtLeast(1));
110
EXPECT_CALL(mock, create_syslog_writer()).Times(AtLeast(1));
112
ml::init_for_configuration(config);
116
DefaultSynchronousConfiguration,
117
initializing_with_a_default_synchronous_configuration_sets_up_global_writers)
119
std::shared_ptr<ml::Configuration> config = ml::default_synchronous_configuration();
120
ml::init_for_configuration(config);
122
ml::Writer::default_instance();
123
ml::Writer::syslog_instance();
127
DefaultAsynchronousConfiguration,
128
initializing_with_a_default_asynchronous_configuration_sets_up_global_writers)
130
std::shared_ptr<ml::Configuration> config = ml::default_asynchronous_configuration();
131
ml::init_for_configuration(config);
133
ml::Writer::default_instance();
134
ml::Writer::syslog_instance();
138
DefaultAsynchronousConfiguration,
139
initializing_with_threading_hint_to_start_own_thread_switches_config_to_state_running)
141
auto async_config = ml::default_asynchronous_configuration();
143
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
145
EXPECT_TRUE(async_config->is_running());
149
DefaultAsynchronousConfiguration,
150
initializing_with_threading_hint_to_not_start_own_thread_does_not_switch_config_automatically)
152
auto async_config = ml::default_asynchronous_configuration(
153
ml::AsynchronousConfiguration::do_not_run_in_own_thread);
155
EXPECT_FALSE(async_config->is_running());
159
&ml::AsynchronousConfiguration::run,
160
async_config.get()));
162
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
164
EXPECT_TRUE(async_config->is_running());
166
async_config->stop();
169
EXPECT_FALSE(async_config->is_running());