Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_LOGGER_HPP
2#define PROTEUS_LOGGER_HPP
3
4#include <filesystem>
5#include <stdexcept>
6#include <string>
7#include <system_error>
8#include <unistd.h>
9
10#include <llvm/Support/Error.h>
11#include <llvm/Support/FileSystem.h>
12#include <llvm/Support/raw_ostream.h>
13
14namespace proteus {
15
16class Logger {
17public:
18 static llvm::raw_ostream &logs(const std::string &Name) {
19 static Logger SingletonLogger{Name};
20 return SingletonLogger.OutStream;
21 }
22
23 template <typename T>
24 static void logfile(const std::string &Filename, T &&Data) {
25 std::error_code EC;
26 llvm::raw_fd_ostream Out(std::string(LogDir) + "/" +
27 std::to_string(getpid()) + "." + Filename,
28 EC);
29 if (EC)
30 throw std::runtime_error("Error opening file: " + EC.message());
31 Out << Data;
32 Out.close();
33 }
34
35private:
36 static constexpr char LogDir[] = ".proteus-logs";
37 bool DirExists;
38 std::error_code EC;
39 llvm::raw_fd_ostream OutStream;
40
41 Logger(const std::string &Name)
42 : DirExists(std::filesystem::create_directory(LogDir)),
43 OutStream(llvm::raw_fd_ostream{std::string(LogDir) + "/" + Name + "." +
44 std::to_string(getpid()) + ".log",
45 EC, llvm::sys::fs::OF_None}) {
46 if (EC)
47 throw std::runtime_error("Error opening file: " + EC.message());
48 }
49};
50
51} // namespace proteus
52
53#endif
Definition Logger.hpp:16
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:18
static void logfile(const std::string &Filename, T &&Data)
Definition Logger.hpp:24
Definition JitEngine.cpp:20
Definition Hashing.hpp:94