diff --git a/example/example.cpp b/example/example.cpp index 99c18993..692f8e9a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -108,7 +108,7 @@ void async_example() //syslog example (linux/osx/freebsd) void syslog_example() { -#if defined (__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#ifdef SPDLOG_ENABLE_SYSLOG std::string ident = "spdlog-example"; auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID); syslog_logger->warn("This is warning that will end up in syslog."); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 490deec7..7885292e 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -37,6 +37,16 @@ #define DEPRECATED #endif +#include + +#ifndef SPDLOG_ENABLE_SYSLOG +#if defined (__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#define SPDLOG_ENABLE_SYSLOG +#elif defined(sun) || defined(__sun) +#define SPDLOG_ENABLE_SYSLOG +#endif +#endif + #include diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index ccadaea3..a6fbf6d9 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -198,12 +198,12 @@ inline size_t filesize(FILE *f) throw spdlog_ex("Failed getting file size. fd is null"); #ifdef _WIN32 int fd = _fileno(f); -#if _WIN64 //64 bits +#if _WIN64 //64 bits struct _stat64 st; if (_fstat64(fd, &st) == 0) return st.st_size; -#else //windows 32 bits +#else //windows 32 bits struct _stat st; if (_fstat(fd, &st) == 0) return st.st_size; @@ -216,7 +216,7 @@ inline size_t filesize(FILE *f) struct stat64 st; if (fstat64(fd, &st) == 0) return st.st_size; -#else // unix 32 bits or osx +#else // unix 32 bits or osx struct stat st; if (fstat(fd, &st) == 0) return st.st_size; @@ -250,7 +250,42 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime()) offset -= tzinfo.StandardBias; return offset; #else - return static_cast(tm.tm_gmtoff / 60); + long int offset_seconds; + +#if defined(sun) || defined(__sun) + // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris + struct helper { + static long int calculate_gmt_offset(const std::tm & localtm = details::os::localtime(), const std::tm & gmtm = details::os::gmtime()) { + int local_year = localtm.tm_year + (1900 - 1); + int gmt_year = gmtm.tm_year + (1900 - 1); + + long int days = ( + // difference in day of year + localtm.tm_yday - gmtm.tm_yday + + // + intervening leap days + + ((local_year >> 2) - (gmt_year >> 2)) + - (local_year / 100 - gmt_year / 100) + + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) + + // + difference in years * 365 */ + + (long int)(local_year - gmt_year) * 365 + ); + + long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); + long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); + long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec); + + return secs; + } + }; + + offset_seconds = helper::calculate_gmt_offset(tm); +#else + offset_seconds = tm.tm_gmtoff; +#endif + + return static_cast(offset_seconds / 60); #endif } @@ -269,7 +304,7 @@ inline size_t thread_id() long tid; thr_self(&tid); return static_cast(tid); -#else //Default to standard C++11 (OSX and other Unix) +#else //Default to standard C++11 (OSX and other Unix) return static_cast(std::hash()(std::this_thread::get_id())); #endif diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index b6ba5ad0..4ef085b6 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -96,7 +96,7 @@ inline std::shared_ptr spdlog::stderr_logger_st(const std::strin return create_console_logger(logger_name, sinks::stderr_sink_st::instance(), color); } -#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#ifdef SPDLOG_ENABLE_SYSLOG // Create syslog logger inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name, const std::string& syslog_ident, int syslog_option) { diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 1c571ef7..4c5b6876 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -5,10 +5,11 @@ #pragma once -#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#include + +#ifdef SPDLOG_ENABLE_SYSLOG #include -#include #include #include diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 8a0ba08f..ff1b6022 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -98,7 +98,7 @@ std::shared_ptr stderr_logger_st(const std::string& logger_name, bool co // // Create and register a syslog logger // -#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) +#ifdef SPDLOG_ENABLE_SYSLOG std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0); #endif diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 1738fb93..64bad92f 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -11,6 +11,14 @@ // /////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +// By default, syslog sink is only enabled on tested operating systems. +// Uncomment to enable it unconditionally. +// +// #define SPDLOG_ENABLE_SYSLOG +/////////////////////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////////////////// // Under Linux, the much faster CLOCK_REALTIME_COARSE clock can be used. // This clock is less accurate - can be off by dozens of millis - depending on the kernel HZ.