From 714ed292b37a0d9baee69186bca38cf0f0c77c89 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 16 Nov 2017 21:33:21 +0000 Subject: Before setting license --- .gitignore | 3 +++ scripts/add_version_headers | 43 +++++++++++++++++++++------------- yage/core/.#logmessage.h | 1 - yage/core/logger.cpp | 1 + yage/core/logsink.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++-- yage/core/logsink.h | 3 +++ 6 files changed, 89 insertions(+), 19 deletions(-) delete mode 120000 yage/core/.#logmessage.h diff --git a/.gitignore b/.gitignore index 2232101c..991dcdfa 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ Makefile GPATH GTAGS GRTAGS + +# do not upload log files +*.log diff --git a/scripts/add_version_headers b/scripts/add_version_headers index ffbdd036..2d653675 100755 --- a/scripts/add_version_headers +++ b/scripts/add_version_headers @@ -37,6 +37,10 @@ header = """/* ---------------------------------------------------------------\ """ +lic_regex = re.compile("\/\* -+\n \* [a-z]+\.cpp\n(?: \*[ a-zA-Z0-9_.,\-()<>@]*\n)+ \* -+\n \*/", + re.MULTILINE) + + class HeaderUpdate(object): """Updates the header in all the source and header files in the code""" def __init__(self, **kwargs): @@ -45,24 +49,34 @@ class HeaderUpdate(object): self.exclude_build = True self.exclude_dir = ".*build.*" self.match_dir = "" - self.comment_match = "^\/\* -+" self.starting_dir = os.getcwd() for key, value in kwargs.items(): setattr(self, key, value) def writeHeader(self): - def writeFileOperation(filePath, src): - if not re.match(self.comment_match, src): - print(filePath, end=" ") - with open(filePath, 'w') as src_file_lic: - src_file_lic.write(header.format(filePath)) - src_file_lic.write(src) - print("-- done") + def writeFileOperation(filePath): + with open(filePath, "r+") as f: + src = f.read() + f.seek(0) + if lic_regex.match(src): + print(filePath) + f.write(src) + f.truncate() self._traverseDir(self.starting_dir, writeFileOperation) def updateHeader(self): - ... + def updateFileOperation(filePath): + with open(filePath, "r+") as f: + src = f.read() + f.seek(0) + print("Updating {}:".format(os.path.basename(filePath)), end="") + lic_regex.sub(header.format(os.path.basename(filePath)), src) + print("done") + f.write(src) + f.truncate() + + self._traverseDir(self.starting_dir, updateFileOperation) def removeHeader(self): ... @@ -71,20 +85,17 @@ class HeaderUpdate(object): for subdir, dirs, files in os.walk(os.getcwd()): if (re.match(self.match_dir, subdir)) and \ (not re.match(self.exclude_dir, subdir)): - print(subdir) for fileName in files: if (re.match(self.match_re, fileName)) and \ (not re.match(self.exclude_re, fileName)): - with open(os.path.join(subdir, fileName), 'r') \ - as src_file: - src = src_file.read() - fileOperation(os.path.join(subdir, fileName), src) + fileOperation(os.path.join(subdir, fileName)) def main(argv): update = HeaderUpdate(exclude_re="^picopng\.cpp", - match_dir=".*yage|.*tests") - update.writeHeader() + match_dir=".*yage|.*tests", + exclude_dir=".*lib.*|.*build.*") + update.updateHeader() if __name__ == "__main__": diff --git a/yage/core/.#logmessage.h b/yage/core/.#logmessage.h deleted file mode 120000 index c4ccefcf..00000000 --- a/yage/core/.#logmessage.h +++ /dev/null @@ -1 +0,0 @@ -yannherklotz@yann-arch.2036:1510699559 \ No newline at end of file diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp index 12a23dfc..a0114a66 100644 --- a/yage/core/logger.cpp +++ b/yage/core/logger.cpp @@ -20,6 +20,7 @@ namespace yage Logger::Logger() : active_(Active::create()) { add(makeConsoleSink()); + add(makeFileSink("yage.log")); } LogMessage Logger::operator()(const std::string &fileName, int lineNum) diff --git a/yage/core/logsink.cpp b/yage/core/logsink.cpp index a8f4d7b4..081eb5b3 100644 --- a/yage/core/logsink.cpp +++ b/yage/core/logsink.cpp @@ -8,6 +8,10 @@ #include "logsink.h" +#include +#include +#include +#include #include namespace yage @@ -42,9 +46,58 @@ void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) const LogSink makeConsoleSink() { return [](const LogMessage::Meta &meta, const std::string &msg) { - std::cout << msg << " (" << meta.fileName << ":" << meta.lineNo - << ")\n"; + std::cout << msg << "\n"; }; } +namespace +{ + +class FileSink +{ +public: + FileSink(std::string &&filename) + : fileHandle_(std::make_shared(filename)) + { + if (!fileHandle_->good()) { + throw std::runtime_error("Could not open file: " + filename); + } + } + + FileSink(const std::string filename) + : fileHandle_(std::make_shared(filename)) + { + if (!fileHandle_->good()) { + throw std::runtime_error("Could not open file: " + filename); + } + } + + ~FileSink() = default; + + void operator()(const LogMessage::Meta &meta, const std::string &msg) const + { + using namespace std::chrono; + + auto now = system_clock::now(); + auto time_t = system_clock::to_time_t(now); + auto local_time = std::localtime(&time_t); + + (*fileHandle_) << std::put_time(local_time, "[%H:%M:%S] ") << msg + << " (" << meta.fileName << ":" << meta.lineNo << ")\n"; + } + +private: + std::shared_ptr fileHandle_; +}; + +} // namespace + +LogSink makeFileSink(const std::string &filename) { + return FileSink(filename); +} + +LogSink makeFileSink(std::string &&filename) { + return FileSink(filename); +} + } // namespace yage diff --git a/yage/core/logsink.h b/yage/core/logsink.h index 526c862e..f51d9f2a 100644 --- a/yage/core/logsink.h +++ b/yage/core/logsink.h @@ -56,6 +56,9 @@ private: LogSink makeConsoleSink(); +LogSink makeFileSink(const std::string &filename); +LogSink makeFileSink(std::string &&filename); + /* ----------------------------------------------------------------------------- * Template Implementation * ----------------------------------------------------------------------------- -- cgit