Make agi::util::time_log less insane

This commit is contained in:
Thomas Goyne 2013-01-21 21:31:35 -08:00
parent ca9be4caef
commit 69e1744fc7
10 changed files with 62 additions and 111 deletions

View File

@ -57,7 +57,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\scoped_ptr.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\signal.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\thesaurus.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\types.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util_win.h" />
@ -78,6 +77,7 @@
<ClInclude Include="$(SrcDir)include\libaegisub\fs_fwd.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\dispatch.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\split.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\time.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(SrcDir)windows\lagi_pre.cpp">

View File

@ -74,9 +74,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\thesaurus.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\types.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\util.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -152,6 +149,9 @@
<ClInclude Include="$(SrcDir)include\libaegisub\split.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\time.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(SrcDir)windows\lagi_pre.cpp">

View File

@ -23,7 +23,6 @@
#include "libaegisub/cajun/elements.h"
#include "libaegisub/cajun/writer.h"
#include "libaegisub/io.h"
#include "libaegisub/types.h"
#include "libaegisub/util.h"
#include <algorithm>
@ -45,7 +44,7 @@ const char *Severity_ID = "EAWID";
SinkMessage::SinkMessage(const char *section, Severity severity,
const char *file, const char *func, int line,
agi_timeval tv)
timeval tv)
: section(section)
, severity(severity)
, file(file)
@ -97,9 +96,7 @@ Message::Message(const char *section,
, buf(new char[len])
, msg(buf, len)
{
agi_timeval tv;
util::time_log(tv);
sm = new SinkMessage(section, severity, file, func, line, tv);
sm = new SinkMessage(section, severity, file, func, line, util::time_log());
}
Message::~Message() {
@ -109,18 +106,17 @@ Message::~Message() {
}
JsonEmitter::JsonEmitter(agi::fs::path const& directory, const agi::log::LogSink *log_sink)
: directory(directory)
: time_start(util::time_log())
, directory(directory)
, log_sink(log_sink)
{
util::time_log(time_start);
}
JsonEmitter::~JsonEmitter() {
json::Object root;
json::Array &array = root["log"];
agi_timeval time_close;
util::time_log(time_close);
auto time_close = util::time_log();
Sink const& sink = *log_sink->GetSink();
for (unsigned int i=0; i < sink.size(); i++) {

View File

@ -17,7 +17,7 @@
/// @ingroup libaegisub
#include <libaegisub/fs_fwd.h>
#include <libaegisub/types.h>
#include <libaegisub/time.h>
#include <boost/filesystem/path.hpp>
#include <cstdint>
@ -80,8 +80,7 @@ public:
/// @param func Function name
/// @param line Source line
/// @param tv Log time
SinkMessage(const char *section, Severity severity, const char *file,
const char *func, int line, agi_timeval tv);
SinkMessage(const char *section, Severity severity, const char *file, const char *func, int line, timeval tv);
/// Destructor
~SinkMessage();
@ -144,10 +143,10 @@ public:
/// A simple emitter which writes the log to a file in json format when it's destroyed
class JsonEmitter : public Emitter {
/// Init time
agi_timeval time_start;
timeval time_start;
/// Directory to write the log file in
agi::fs::path directory;
const agi::fs::path directory;
/// Parent sink to get messages from
const agi::log::LogSink *log_sink;

View File

@ -0,0 +1,34 @@
// 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

@ -1,76 +0,0 @@
// Copyright (c) 2010, Amar Takhar <verm@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.
/// @file types.h
/// @brief Platform specific types.
/// @ingroup libaegisub
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
# include <time.h>
#endif
#pragma once
namespace agi {
namespace type {
namespace win {
typedef struct timeval {
long tv_sec; ///< seconds
long tv_usec; ///< microseconds
} timeval;
typedef struct tm {
int tm_sec; ///< seconds (0-59)
int tm_min; ///< minutes (0-59)
int tm_hour; ///< hours (0-23)
int tm_mday; ///< day of the month (1-31)
int tm_mon; ///< months since january (0-11)
int tm_year; ///< years since 1900
int tm_wday; ///< day of the week since sunday (0-6)
int tm_yday; ///< days since january 1 (0-365)
int tm_isdst; ///< whether in DST or not
long tm_gmtoff; ///< GMT offset in seconds
char *tm_zone; ///< TZ abrivation
} tm;
} // namespace win
// u_nix beacuse some compilers set "unix" to 1 if it's unix. -> ARGH.
namespace u_nix {
} // namespace unix
namespace osx {
} // namespace osx
} // namespace type
} // namespace agi
#ifdef _WIN32
typedef agi::type::win::timeval agi_timeval;
#else // Unix / OSX
typedef timeval agi_timeval;
#endif // if _WIN32

View File

@ -16,12 +16,12 @@
/// @brief Public interface for general utilities.
/// @ingroup libaegisub
#include <libaegisub/time.h>
#include <algorithm>
#include <cstdint>
#include <string>
#include <libaegisub/types.h>
struct tm;
namespace agi {
@ -30,8 +30,7 @@ namespace agi {
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.
/// @param tv timeval
void time_log(agi_timeval &tv);
agi_timeval time_log();
bool try_parse(std::string const& str, double *out);
bool try_parse(std::string const& str, int *out);

View File

@ -22,8 +22,10 @@
namespace agi { namespace util {
void time_log(timeval &tv) {
gettimeofday(&tv, (struct timezone *)NULL);
timeval time_log() {
timeval tv;
gettimeofday(&tv, nullptr);
return tv;
}
} }

View File

@ -16,12 +16,12 @@
/// @brief Windows utility methods.
/// @ingroup libaegisub windows
#include "../config.h"
#include "libaegisub/util.h"
#include "libaegisub/util_win.h"
#include <string>
#include "libaegisub/charset_conv_win.h"
#include "libaegisub/types.h"
namespace agi {
namespace util {
@ -44,13 +44,8 @@ std::string ErrorString(DWORD error) {
/// @brief Get seconds and microseconds.
/// @param tv[out] agi_timeval struct
/// This code is from http://www.suacommunity.com/dictionary/gettimeofday-entry.php
void time_log(agi_timeval &tv) {
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
agi_timeval time_log() {
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#endif
// Define a structure to receive the current Windows filetime
FILETIME ft;
@ -75,8 +70,8 @@ void time_log(agi_timeval &tv) {
// Finally change microseconds to seconds and place in the seconds value.
// The modulus picks up the microseconds.
tv.tv_sec = (long)(tmpres / 1000000UL);
tv.tv_usec = (long)(tmpres % 1000000UL);
agi_timeval tv = { (long)(tmpres / 1000000UL), (long)(tmpres % 1000000UL) };
return tv;
}
} // namespace io

View File

@ -14,6 +14,8 @@
//
// Aegisub Project http://www.aegisub.org/
#include "../libaegisub/config.h"
#include <libaegisub/util.h>
#include "main.h"