citrun

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

log.h (1110B)


      1 #ifndef _INST_LOG_H
      2 #define _INST_LOG_H
      3 
      4 #include <llvm/Support/raw_ostream.h>
      5 #include <llvm/Support/FileSystem.h>	// llvm::sys::fs::F_Append
      6 #include <sstream>
      7 #ifdef WIN32
      8 #include <process.h>			// getpid
      9 #else
     10 #include <unistd.h>			// getpid
     11 #endif
     12 
     13 //
     14 // Taken from StackOverflow user Loki Astari. Thanks.
     15 //
     16 class InstrumentLogger : public std::ostream
     17 {
     18 	class LogBuffer : public std::stringbuf
     19 	{
     20 		int			 m_pid;
     21 		llvm::raw_ostream	*m_out;
     22 	public:
     23 
     24 		LogBuffer(bool is_citrun_inst) :
     25 			m_pid(getpid())
     26 		{
     27 			if (is_citrun_inst) {
     28 				m_out = &llvm::outs();
     29 				return;
     30 			}
     31 
     32 			std::error_code m_ec;
     33 			m_out = new llvm::raw_fd_ostream("citrun.log", m_ec,
     34 				llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text);
     35 			if (m_ec.value()) {
     36 				m_out = &llvm::errs();
     37 				*m_out << "Can't open citrun.log: " << m_ec.message();
     38 			}
     39 		}
     40 
     41 		virtual int sync()
     42 		{
     43 			*m_out << m_pid << ": " << str();
     44 			m_out->flush();
     45 			str("");
     46 
     47 			return 0;
     48 		}
     49 	};
     50 
     51 	LogBuffer buffer;
     52 public:
     53 	InstrumentLogger(bool is_citrun_inst) :
     54 		std::ostream(&buffer),
     55 		buffer(is_citrun_inst)
     56 	{
     57 	}
     58 };
     59 
     60 #endif // _INST_LOG_H_