Replace timeval junk with std::chrono

This commit is contained in:
Thomas Goyne 2014-05-25 18:16:28 -07:00
parent 650cfcb043
commit 1f7c47239b
10 changed files with 33 additions and 142 deletions

View File

@ -80,7 +80,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\spellchecker.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\spellchecker.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\split.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\split.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\thesaurus.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\thesaurus.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\time.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\util.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\vfr.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\vfr.h" />

View File

@ -152,9 +152,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\split.h"> <ClInclude Include="$(SrcDir)include\libaegisub\split.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\time.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\ass\uuencode.h"> <ClInclude Include="$(SrcDir)include\libaegisub\ass\uuencode.h">
<Filter>ASS</Filter> <Filter>ASS</Filter>
</ClInclude> </ClInclude>

View File

@ -17,16 +17,15 @@
#include "libaegisub/cajun/elements.h" #include "libaegisub/cajun/elements.h"
#include "libaegisub/cajun/writer.h" #include "libaegisub/cajun/writer.h"
#include "libaegisub/dispatch.h" #include "libaegisub/dispatch.h"
#include "libaegisub/time.h"
#include "libaegisub/util.h" #include "libaegisub/util.h"
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
#include <boost/range/algorithm/remove_if.hpp> #include <boost/range/algorithm/remove_if.hpp>
#include <chrono>
namespace agi { namespace agi { namespace log {
namespace log {
/// Global log sink. /// Global log sink.
LogSink *log; LogSink *log;
@ -75,12 +74,13 @@ decltype(LogSink::messages) LogSink::GetMessages() const {
Message::Message(const char *section, Severity severity, const char *file, const char *func, int line) Message::Message(const char *section, Severity severity, const char *file, const char *func, int line)
: msg(buffer, sizeof buffer) : msg(buffer, sizeof buffer)
{ {
using namespace std::chrono;
sm.section = section; sm.section = section;
sm.severity = severity; sm.severity = severity;
sm.file = file; sm.file = file;
sm.func = func; sm.func = func;
sm.line = line; sm.line = line;
sm.tv = util::time_log(); sm.time = duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
} }
Message::~Message() { Message::~Message() {
@ -91,17 +91,12 @@ Message::~Message() {
JsonEmitter::JsonEmitter(fs::path const& directory) JsonEmitter::JsonEmitter(fs::path const& directory)
: fp(new boost::filesystem::ofstream(unique_path(directory/util::strftime("%Y-%m-%d-%H-%M-%S-%%%%%%%%.json")))) : fp(new boost::filesystem::ofstream(unique_path(directory/util::strftime("%Y-%m-%d-%H-%M-%S-%%%%%%%%.json"))))
{ {
WriteTime("open");
}
JsonEmitter::~JsonEmitter() {
WriteTime("close");
} }
void JsonEmitter::log(SinkMessage *sm) { void JsonEmitter::log(SinkMessage *sm) {
json::Object entry; json::Object entry;
entry["sec"] = (int64_t)sm->tv.tv_sec; entry["sec"] = sm->time / 1000000000;
entry["usec"] = (int64_t)sm->tv.tv_usec; entry["usec"] = sm->time % 1000000000;
entry["severity"] = sm->severity; entry["severity"] = sm->severity;
entry["section"] = sm->section; entry["section"] = sm->section;
entry["file"] = sm->file; entry["file"] = sm->file;
@ -112,14 +107,4 @@ void JsonEmitter::log(SinkMessage *sm) {
fp->flush(); fp->flush();
} }
void JsonEmitter::WriteTime(const char *key) { } }
auto time = util::time_log();
json::Object obj;
json::Array &timeval = obj[key];
timeval.push_back((int64_t)time.tv_sec);
timeval.push_back((int64_t)time.tv_usec);
json::Writer::Write(obj, *fp);
}
} // namespace log
} // namespace agi

View File

@ -12,16 +12,11 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// @file log.h
/// @brief Logging
/// @ingroup libaegisub
#include <libaegisub/fs_fwd.h> #include <libaegisub/fs_fwd.h>
#include <libaegisub/time.h>
#include <boost/circular_buffer.hpp> #include <boost/circular_buffer.hpp>
#include <boost/interprocess/streams/bufferstream.hpp> #include <boost/interprocess/streams/bufferstream.hpp>
#include <iosfwd> #include <cstdint>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -40,8 +35,8 @@
namespace agi { namespace agi {
namespace dispatch { class Queue; } namespace dispatch { class Queue; }
namespace log { namespace log {
class LogSink; class LogSink;
/// Severity levels /// Severity levels
@ -63,7 +58,7 @@ extern LogSink *log;
/// Container to hold a single message /// Container to hold a single message
struct SinkMessage { struct SinkMessage {
std::string message; ///< Formatted message std::string message; ///< Formatted message
agi_timeval tv; ///< Time at execution int64_t time; ///< Time at execution in nanoseconds since epoch
const char *section; ///< Section info eg "video/open" "video/seek" etc const char *section; ///< Section info eg "video/open" "video/seek" etc
const char *file; ///< Source file const char *file; ///< Source file
const char *func; ///< Function name const char *func; ///< Function name
@ -115,14 +110,10 @@ public:
class JsonEmitter final : public Emitter { class JsonEmitter final : public Emitter {
std::unique_ptr<std::ostream> fp; std::unique_ptr<std::ostream> fp;
void WriteTime(const char *key);
public: public:
/// Constructor /// Constructor
/// @param directory Directory to write the log file in /// @param directory Directory to write the log file in
JsonEmitter(fs::path const& directory); JsonEmitter(fs::path const& directory);
/// Destructor
~JsonEmitter();
void log(SinkMessage *) override; void log(SinkMessage *) override;
}; };

View File

@ -1,34 +0,0 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#pragma once
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
# include <ctime>
#endif
#ifdef _WIN32
// timeval on windows is defined by winsock, which is a bit much to drag in for
// a pair of longs
struct agi_timeval {
long tv_sec;
long tv_usec;
};
#else
typedef timeval agi_timeval;
#endif

View File

@ -12,8 +12,6 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <libaegisub/time.h>
#include <algorithm> #include <algorithm>
#include <boost/range/irange.hpp> #include <boost/range/irange.hpp>
#include <string> #include <string>
@ -25,9 +23,6 @@ namespace agi {
/// Clamp `b` to the range [`a`,`c`] /// Clamp `b` to the range [`a`,`c`]
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); } template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
/// Get time suitable for logging mechanisms.
agi_timeval time_log();
bool try_parse(std::string const& str, double *out); bool try_parse(std::string const& str, double *out);
bool try_parse(std::string const& str, int *out); bool try_parse(std::string const& str, int *out);

View File

@ -12,30 +12,31 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "libaegisub/log.h"
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
#include <unistd.h> #include <unistd.h>
#include "libaegisub/log.h"
namespace agi { namespace log { namespace agi { namespace log {
void EmitSTDOUT::log(SinkMessage *sm) { void EmitSTDOUT::log(SinkMessage *sm) {
time_t time = sm->time / 1000000000;
tm tmtime; tm tmtime;
localtime_r(&sm->tv.tv_sec, &tmtime); localtime_r(&time, &tmtime);
printf("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %.*s\n", printf("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %.*s\n",
Severity_ID[sm->severity], Severity_ID[sm->severity],
tmtime.tm_hour, tmtime.tm_hour,
tmtime.tm_min, tmtime.tm_min,
tmtime.tm_sec, tmtime.tm_sec,
(long)sm->tv.tv_usec, (long)(sm->time % 1000000000),
sm->section, sm->section,
sm->file, sm->file,
sm->func, sm->func,
sm->line, sm->line,
(int)sm->message.size(), (int)sm->message.size(),
sm->message.c_str()); sm->message.c_str());
if (!isatty(fileno(stdout))) if (!isatty(fileno(stdout)))
fflush(stdout); fflush(stdout);
} }

View File

@ -23,13 +23,6 @@
#endif #endif
namespace agi { namespace util { namespace agi { namespace util {
timeval time_log() {
timeval tv;
gettimeofday(&tv, nullptr);
return tv;
}
void SetThreadName(const char *) { } void SetThreadName(const char *) { }
void sleep_for(int ms) { void sleep_for(int ms) {

View File

@ -12,10 +12,6 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// @file util.cpp
/// @brief Windows utility methods.
/// @ingroup libaegisub windows
#include "libaegisub/util.h" #include "libaegisub/util.h"
#include "libaegisub/charset_conv_win.h" #include "libaegisub/charset_conv_win.h"
@ -43,39 +39,6 @@ std::string ErrorString(int error) {
return str; return str;
} }
/// @brief Get seconds and microseconds.
/// @param tv[out] agi_timeval struct
/// This code is from http://www.suacommunity.com/dictionary/gettimeofday-entry.php
agi_timeval time_log() {
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
// Define a structure to receive the current Windows filetime
FILETIME ft;
// Initialize the present time to 0 and the timezone to UTC
unsigned __int64 tmpres = 0;
GetSystemTimeAsFileTime(&ft);
// The GetSystemTimeAsFileTime returns the number of 100 nanosecond
// intervals since Jan 1, 1601 in a structure. Copy the high bits to
// the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
// Convert to microseconds by dividing by 10
tmpres /= 10;
// The Unix epoch starts on Jan 1 1970. Need to subtract the difference
// in seconds from Jan 1 1601.
tmpres -= DELTA_EPOCH_IN_MICROSECS;
// Finally change microseconds to seconds and place in the seconds value.
// The modulus picks up the microseconds.
agi_timeval tv = { (long)(tmpres / 1000000UL), (long)(tmpres % 1000000UL) };
return tv;
}
#define MS_VC_EXCEPTION 0x406d1388 #define MS_VC_EXCEPTION 0x406d1388
/// Parameters for setting the thread name /// Parameters for setting the thread name

View File

@ -53,15 +53,16 @@ public:
} }
void log(agi::log::SinkMessage *sm) override { void log(agi::log::SinkMessage *sm) override {
time_t time = sm->time / 1000000000;
#ifndef _WIN32 #ifndef _WIN32
tm tmtime; tm tmtime;
localtime_r(&sm->tv.tv_sec, &tmtime); localtime_r(&time, &tmtime);
auto log = wxString::Format("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %s\n", auto log = wxString::Format("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %s\n",
agi::log::Severity_ID[sm->severity], agi::log::Severity_ID[sm->severity],
(int)tmtime.tm_hour, (int)tmtime.tm_hour,
(int)tmtime.tm_min, (int)tmtime.tm_min,
(int)tmtime.tm_sec, (int)tmtime.tm_sec,
(long)sm->tv.tv_usec, (long)(sm->time % 1000000000),
sm->section, sm->section,
sm->file, sm->file,
sm->func, sm->func,
@ -70,7 +71,7 @@ public:
#else #else
auto log = wxString::Format("%c %-6ld <%-25s> [%s:%s:%d] %s\n", auto log = wxString::Format("%c %-6ld <%-25s> [%s:%s:%d] %s\n",
agi::log::Severity_ID[sm->severity], agi::log::Severity_ID[sm->severity],
sm->tv.tv_usec, (long)(sm->time % 1000000000),
sm->section, sm->section,
sm->file, sm->file,
sm->func, sm->func,