citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 9202f46d8dd2e7db1fb65188600e6a118740c6cc
parent 1813388f4e203b5a62687049864ed9be271366ab
Author: Kyle Milz <kyle@0x30.net>
Date:   Tue, 30 Aug 2016 19:02:53 -0600

src: always write citrun.log file and sometimes tee to console

Diffstat:
Msrc/inst_frontend.cc | 6+++---
Msrc/inst_log.cc | 38++++++++++++++------------------------
Msrc/inst_log.h | 31++++++++++++++++++-------------
3 files changed, 35 insertions(+), 40 deletions(-)

diff --git a/src/inst_frontend.cc b/src/inst_frontend.cc @@ -193,7 +193,7 @@ InstrumentFrontend::instrument() clang::DiagnosticOptions diags; clang::TextDiagnosticPrinter *log; - log = new clang::TextDiagnosticPrinter(*m_log->m_output, &diags, false); + log = new clang::TextDiagnosticPrinter(m_log->m_outfile, &diags, false); log->setPrefix(std::to_string(m_log->m_pid)); Tool.setDiagnosticConsumer(log); @@ -218,7 +218,7 @@ void InstrumentFrontend::exec_compiler() { // XXX: Need to destroy log here. - m_log->m_output->flush(); + m_log->m_outfile.flush(); if (m_is_citruninst) { *m_log << "Running as citrun-inst, not re-exec()'ing\n"; @@ -234,7 +234,7 @@ int InstrumentFrontend::fork_compiler() { // Otherwise we'll get two copies of buffers after fork(). - m_log->m_output->flush(); + m_log->m_outfile.flush(); pid_t child_pid; if ((child_pid = fork()) < 0) diff --git a/src/inst_log.cc b/src/inst_log.cc @@ -1,39 +1,29 @@ #include "inst_log.h" +#include <iostream> #include <llvm/Support/FileSystem.h> // llvm::sys::fs::F_Append -#include <err.h> InstrumentLogger::InstrumentLogger(const bool &is_citruninst) : - m_pid(getpid()), + m_iscitruninst(is_citruninst), m_needs_prefix(true), - m_delete(false) -{ - if (is_citruninst) { - m_output = &llvm::outs(); - } else { - std::error_code ec; - m_output = new llvm::raw_fd_ostream("citrun.log", ec, llvm::sys::fs::F_Append); - m_delete = true; - - if (ec.value()) { - warnx("citrun.log: %s", ec.message().c_str()); - m_output = &llvm::nulls(); - m_delete = false; - } - } -} - -InstrumentLogger::~InstrumentLogger() + m_pid(getpid()), + m_ec(), + m_outfile("citrun.log", m_ec, llvm::sys::fs::F_Append) { - if (m_delete) - delete m_output; + if (m_ec.value()) + std::cerr << "Can't open citrun.log: " << m_ec.message(); } InstrumentLogger& operator<<(InstrumentLogger& out, const char *rhs) { + if (out.m_ec.value()) + return out; + out.print_prefix(); - *out.m_output << rhs; + out.m_outfile << rhs; + if (out.m_iscitruninst) + llvm::outs() << rhs; out.check_newline(rhs); return out; @@ -43,7 +33,7 @@ void InstrumentLogger::print_prefix() { if (m_needs_prefix) { - *m_output << m_pid << ": "; + m_outfile << m_pid << ": "; m_needs_prefix = false; } } diff --git a/src/inst_log.h b/src/inst_log.h @@ -1,32 +1,37 @@ -#ifndef __INST_LOG_H_ -#define __INST_LOG_H_ +#ifndef _INST_LOG_H +#define _INST_LOG_H #include <llvm/Support/raw_ostream.h> #include <unistd.h> // pid_t class InstrumentLogger { +private: + void print_prefix(); + void check_newline(const std::string &); + + bool m_iscitruninst; + bool m_needs_prefix; + public: InstrumentLogger(const bool &); - ~InstrumentLogger(); template <typename T> friend InstrumentLogger& operator<<(InstrumentLogger& out, const T &rhs) { + if (out.m_ec.value()) + return out; + out.print_prefix(); - *out.m_output << rhs; + out.m_outfile << rhs; + if (out.m_iscitruninst) + llvm::outs() << rhs; return out; } friend InstrumentLogger& operator<<(InstrumentLogger&, const char *); - pid_t m_pid; - llvm::raw_ostream *m_output; - -private: - void print_prefix(); - void check_newline(const std::string &); - - bool m_needs_prefix; - bool m_delete; + pid_t m_pid; + std::error_code m_ec; + llvm::raw_fd_ostream m_outfile; }; #endif // _INST_LOG_H_