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 <iostream>
6#include <stdexcept>
7#include <string>
8#include <system_error>
9#include <unistd.h>
10
11#include <llvm/Support/Error.h>
12#include <llvm/Support/FileSystem.h>
13#include <llvm/Support/raw_ostream.h>
14
15namespace proteus {
16
17class Logger {
18public:
19 static llvm::raw_ostream &logs(const std::string &Name) {
20 static Logger SingletonLogger{Name};
21 SingletonLogger.OutStream << "[" << Name << "] ";
22 return SingletonLogger.OutStream;
23 }
24
25 static llvm::raw_ostream &outs(const std::string &Name) {
26 llvm::outs() << "[" << Name << "] ";
27 return llvm::outs();
28 }
29
30 static void trace(llvm::StringRef Msg) { std::cout << Msg.str(); }
31
32 template <typename T>
33 static void logfile(const std::string &Filename, T &&Data) {
34 std::error_code EC;
35 llvm::raw_fd_ostream Out(std::string(LogDir) + "/" +
36 std::to_string(getpid()) + "." + Filename,
37 EC);
38 if (EC)
39 throw std::runtime_error("Error opening file: " + EC.message());
40 Out << Data;
41 Out.close();
42 }
43
44private:
45 static constexpr char LogDir[] = ".proteus-logs";
46 bool DirExists;
47 std::error_code EC;
48 llvm::raw_fd_ostream OutStream;
49
50 Logger(const std::string &Name)
51 : DirExists(std::filesystem::create_directory(LogDir)),
52 OutStream(llvm::raw_fd_ostream{std::string(LogDir) + "/" + Name + "." +
53 std::to_string(getpid()) + ".log",
54 EC, llvm::sys::fs::OF_None}) {
55 if (EC)
56 throw std::runtime_error("Error opening file: " + EC.message());
57
58 // Synchronize C++ streams with stdio for tracing (e.g., printf from GPU
59 // kernels).
60 std::ios::sync_with_stdio(true);
61 }
62};
63
64} // namespace proteus
65
66#endif
Definition Logger.hpp:17
static llvm::raw_ostream & outs(const std::string &Name)
Definition Logger.hpp:25
static void trace(llvm::StringRef Msg)
Definition Logger.hpp:30
static llvm::raw_ostream & logs(const std::string &Name)
Definition Logger.hpp:19
static void logfile(const std::string &Filename, T &&Data)
Definition Logger.hpp:33
Definition Dispatcher.cpp:14
Definition Hashing.hpp:94