Skip to content

Commit

Permalink
refs #41 @4h added facility factory to logger::syslog_facility, facil…
Browse files Browse the repository at this point in the history
…ities has moved out of logger/definitions.hpp into dedicated source code files.

> **WARN** we failed to get GMock running.
  • Loading branch information
HerbertKoelman committed Apr 14, 2019
1 parent 8d1a38e commit 972d9de
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 64 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(LOGGER_SOURCE
src/stderr_sink.cpp
src/stdout_sink.cpp
src/syslog_sink.cpp
)
src/facilities.cpp include/logger/facilities.hpp)

add_library(cpp-logger-static STATIC ${LOGGER_SOURCE})
target_link_libraries(cpp-logger-static pthread)
Expand All @@ -74,9 +74,6 @@ endif(BUILD_TESTS)
#
find_package(Doxygen REQUIRED dot OPTIONAL_COMPONENTS mscgen dia)
if (Doxygen_FOUND)
# set(DOXYGEN_OUTPUT_DIRECTORY doxygen)
# set(DOXYGEN_GENERATE_MAN YES)
# set(DOXYGEN_GENERATE_HTML YES)
set(DOXYGEN_EXAMPLE_PATH tests)
set(DOXYGEN_PROJECT_BRIEF "Simple C++ wrapper to pthread functions.")
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md")
Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ rights of fair use or other equivalent, as provided by copyright law.
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
with log_facility for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
Expand Down Expand Up @@ -279,7 +279,7 @@ in one of these ways:
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
that supports equivalent copying log_facility, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
Expand Down
1 change: 1 addition & 0 deletions include/logger/cpp-logger.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <logger/logger.hpp>
#include <logger/facilities.hpp>
#include <logger/registry.hpp>
#include <logger/sinks.hpp>

Expand Down
65 changes: 28 additions & 37 deletions include/logger/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,34 @@ namespace logger {
/** used to define method paramters (readability) */
typedef log_levels log_level;

/** known facilities */
class log_facility {
public:
virtual const int code() {
return _code;
}

virtual const std::string &keyword() {
return _keyword;
}

virtual const std::string &description(){
return _description;
}

log_facility( int code, std::string keyword, std::string description = ""): _code(code), _keyword(keyword), _description(description){
// intentional...
}

virtual ~log_facility(){

}
private:
int _code;
std::string _keyword;
std::string _description;
};

/** RFC5424 log facility.
*
*/
class syslog_user_facility: public log_facility{
public:
syslog_user_facility():log_facility(1, "user", "User-level messages"){

}
};
// TODO remove this as soon as pmossible
// /** known facilities */
// class log_facility {
// public:
// virtual const int code() {
// return _code;
// }
//
// virtual const std::string &keyword() {
// return _keyword;
// }
//
// virtual const std::string &description(){
// return _description;
// }
//
// log_facility( int code, std::string keyword, std::string description = ""): _code(code), _keyword(keyword), _description(description){
// // intentional...
// }
//
// virtual ~log_facility(){
//
// }
// private:
// int _code;
// std::string _keyword;
// std::string _description;
// };

enum log_facilities {
sic_bat, //!< pour les batchs
Expand Down
15 changes: 8 additions & 7 deletions include/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,14 @@ namespace logger {
std::string name() const;

/** @return logger's facility (see logger::log_facility) */
const log_facility *facility() const;

/** modifie la facilit. . utiliser.
*
* @param facility facility to use
*/
void set_facility(log_facility *facility);
const log_facility_ptr facility() const;

// /** modifie la facilit. . utiliser.
// *
// * @param facility facility to use
// */
// TODO check if we should allow to change logger's facility or if this can only be done once when a logger instance is created
// void set_facility(log_facility *facility);

/** create a logger instance.
*
Expand Down
11 changes: 7 additions & 4 deletions include/logger/sinks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* author: herbert koelman ([email protected])
*/

//#include "logger/config.h"
#include "logger/facilities.hpp"

#include <mutex>
#include <shared_mutex>
Expand All @@ -19,6 +19,7 @@
#define CPP_LOGGER_SINKS_HPP

#include "logger/definitions.hpp"
#include <logger/facilities.hpp>

#define MAXECIDLEN 64

Expand Down Expand Up @@ -74,15 +75,16 @@ namespace logger {
};

/** @return logger's facility (see logger::log_facility) */
const log_facility *facility() const {
const log_facility_ptr facility() const {

return _facility;
};

/** set faclity name
*
* @param facility facility name
*/
void set_facility(log_facility *facility);
void set_facility(log_facility_ptr facility);

/** change the current ecid.
*
Expand Down Expand Up @@ -122,10 +124,11 @@ namespace logger {
std::string _name; //!< logging domain name (as for now, this is equal to the logger name)
std::string _pattern;//!< message pattern (layout)
std::string _pname; //!< program name
log_facility *_facility; //!< current facility class
std::string _ecid; //!< execution control ID. Helps to track everything that was logged by one business operation
log_level _level; //!< current logging level

log_facility_ptr _facility; //!< current facility class

private:
std::mutex _mutex; //!< used to protect access to static class data
std::shared_mutex _shared_mutex;
Expand Down
8 changes: 4 additions & 4 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ namespace logger {
}
}

const log_facility *logger::facility() const {
const log_facility_ptr logger::facility() const {
return _sink->facility();
};

void logger::set_facility(log_facility *facility) {
_sink->set_facility(facility);
}
// void logger::set_facility(log_facility *facility) {
// _sink->set_facility(facility);
// }

std::string logger::ecid() {
return _sink->ecid();
Expand Down
6 changes: 3 additions & 3 deletions src/sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace logger {
printf("DEBUG %s (%s,%d).\n", __FUNCTION__, __FILE__, __LINE__);
#endif

_facility = new syslog_user_facility();
_facility = syslog_facility::default_facility();
}

sink::sink(const std::string &name, const std::string &pname, log_facility *facility, log_level level) :
Expand All @@ -37,7 +37,7 @@ sink::sink(const std::string &name, const std::string &pname, log_facility *faci
#endif

if ( facility == nullptr){
_facility = new syslog_user_facility();
_facility = syslog_facility::default_facility();
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ sink::sink(const std::string &name, const std::string &pname, log_facility *faci

}

void sink::set_facility(log_facility *facility) {
void sink::set_facility(log_facility_ptr facility) {

_facility = facility;

Expand Down
7 changes: 5 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
add_executable(logger_tests logger_test.cpp)
target_link_libraries(logger_tests GTest::GTest cpp-logger-static GTest::gtest_main)

add_executable(facility_tests facility_tests.cpp)
target_link_libraries(facility_tests GTest::GTest GMock::GMock cpp-logger-static GTest::gtest_main)

add_executable(logger_performance_tests logger_performance_tests.cpp)
target_link_libraries(logger_performance_tests GTest::GTest cpp-logger-static GTest::gtest_main)

add_test(NAME logger_tests COMMAND logger_tests)

add_test(NAME logger_tests COMMAND logger_tests)
add_test(NAME facility_tests COMMAND facility_tests)
17 changes: 16 additions & 1 deletion tests/logger_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>
#include <syslog.h>
#include "gtest/gtest.h"
// TODO missing GMock include directory - #include "gmock/gmock.h"

/**
* This checks that a registered logger is instanciated only once.
Expand All @@ -24,9 +25,23 @@ TEST(registry, unicity_check) {

TEST(registry, stderr_sink) {
logger::logger_ptr err = logger::get<logger::stderr_sink>("stderr-test-logger");
err->info("stdout sink test (version: %s)", logger::cpp_logger_version());

EXPECT_NE(err, nullptr);
EXPECT_EQ(err->name(), "stderr-test-logger");

::testing::internal::CaptureStderr();

err->info("stdout sink test (version: %s)", logger::cpp_logger_version());

// TODO we fail to find GMock headers and probably libraries. CHeck cmake/GTestExtConfig.txt script to find out why
// std::string output = ::testing::internal::GetCapturedStderr();
// EXPECT_THAT(output, ::testing::HasSubstr("[L SUBSYS=stderr-test-logger] stdout sink test"));

// testing::internal::CaptureStdout();
// std::cout << "My test";
// printf("Says Hello, world");
// std::string output = testing::internal::GetCapturedStdout();
// std::cout << std::endl << "Intercepted: " << output << std::endl ;
}

TEST(registry, stdout_sink) {
Expand Down

0 comments on commit 972d9de

Please sign in to comment.