cleaned common.h and moved some code around

This commit is contained in:
gabime 2016-05-15 01:45:16 +03:00
parent 10d5292bbb
commit 80a432e646
5 changed files with 31 additions and 33 deletions

View File

@ -5,7 +5,6 @@
#pragma once
#include <string>
#include <initializer_list>
#include <chrono>
@ -19,7 +18,6 @@
#include <spdlog/details/null_mutex.h>
//visual studio upto 2013 does not support noexcept nor constexpr
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define SPDLOG_NOEXCEPT throw()
@ -30,7 +28,6 @@
#endif
namespace spdlog
{
@ -41,7 +38,6 @@ namespace sinks
class sink;
}
// Common types across the lib
using log_clock = std::chrono::system_clock;
using sink_ptr = std::shared_ptr < sinks::sink >;
using sinks_init_list = std::initializer_list < sink_ptr >;
@ -115,21 +111,10 @@ private:
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
//
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
#define SPDLOG_FILENAME_T(s) L ## s
using filename_t = std::wstring;
inline std::string filename_to_str(const filename_t& filename)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
return c.to_bytes(filename);
}
#else
#define SPDLOG_FILENAME_T(s) s
using filename_t = std::string;
inline std::string filename_to_str(const filename_t& filename)
{
return filename;
}
#endif
} //spdlog

View File

@ -25,6 +25,7 @@ namespace details
class file_helper
{
public:
const int open_tries = 5;
const int open_interval = 10;
@ -57,7 +58,7 @@ public:
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
}
throw spdlog_ex("Failed opening file " + filename_to_str(_filename) + " for writing");
throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing");
}
void reopen(bool truncate)
@ -88,34 +89,30 @@ public:
size_t msg_size = msg.formatted.size();
auto data = msg.formatted.data();
if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
throw spdlog_ex("Failed writing to file " + filename_to_str(_filename));
throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename));
if (_force_flush)
std::fflush(_fd);
}
long size()
{
if (!_fd)
throw spdlog_ex("Cannot use size() on closed file " + filename_to_str(_filename));
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
auto pos = ftell(_fd);
if (fseek(_fd, 0, SEEK_END) != 0)
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename));
auto file_size = ftell(_fd);
if(fseek(_fd, pos, SEEK_SET) !=0)
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename));
if (file_size == -1)
throw spdlog_ex("ftell failed on file " + filename_to_str(_filename));
throw spdlog_ex("ftell failed on file " + os::filename_to_str(_filename));
return file_size;
}
const filename_t& filename() const
@ -129,14 +126,10 @@ public:
return os::file_exists(name);
}
private:
FILE* _fd;
filename_t _filename;
filename_t _filename;
bool _force_flush;
};
}
}

View File

@ -112,7 +112,7 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
return !(tm1 == tm2);
}
// eol at end of each log line
// eol definition
#if !defined (SPDLOG_EOL)
#ifdef _WIN32
#define SPDLOG_EOL "\r\n"
@ -125,6 +125,7 @@ SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
//fopen_s on non windows for writing
inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
{
@ -228,6 +229,23 @@ inline size_t thread_id()
}
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
#define SPDLOG_FILENAME_T(s) L ## s
inline std::string filename_to_str(const filename_t& filename)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
return c.to_bytes(filename);
}
#else
#define SPDLOG_FILENAME_T(s) s
inline std::string filename_to_str(const filename_t& filename)
{
return filename;
}
#endif
} //os
} //details
} //spdlog

View File

@ -20,7 +20,7 @@
namespace spdlog
{
namespace sinks
{
{
/*
* Trivial file sink with single file as target
*/
@ -108,6 +108,7 @@ private:
void _rotate()
{
using details::os::filename_to_str;
_file_helper.close();
for (auto i = _max_files; i > 0; --i)
{

View File

@ -19,6 +19,7 @@
namespace spdlog
{
// Return an existing logger or nullptr if a logger with such name doesn't exist.
// Examples:
//