]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/opencsd/decoder/source/ocsd_msg_logger.cpp
Re-add opencsd as a vendor import from the dist directory
[FreeBSD/FreeBSD.git] / contrib / opencsd / decoder / source / ocsd_msg_logger.cpp
1 /*
2  * \file       ocsd_msg_logger.cpp
3  * \brief      OpenCSD : 
4  * 
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7
8 /* 
9  * Redistribution and use in source and binary forms, with or without modification, 
10  * are permitted provided that the following conditions are met:
11  * 
12  * 1. Redistributions of source code must retain the above copyright notice, 
13  * this list of conditions and the following disclaimer.
14  * 
15  * 2. Redistributions in binary form must reproduce the above copyright notice, 
16  * this list of conditions and the following disclaimer in the documentation 
17  * and/or other materials provided with the distribution. 
18  * 
19  * 3. Neither the name of the copyright holder nor the names of its contributors 
20  * may be used to endorse or promote products derived from this software without 
21  * specific prior written permission. 
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33  */ 
34
35 #include "common/ocsd_msg_logger.h"
36
37 #include <iostream>
38 #include <sstream>
39
40 #define MSGLOG_OUT_MASK (ocsdMsgLogger::OUT_FILE | ocsdMsgLogger::OUT_STDERR | ocsdMsgLogger::OUT_STDOUT | ocsdMsgLogger::OUT_STR_CB)
41
42 ocsdMsgLogger::ocsdMsgLogger() :
43     m_outFlags(ocsdMsgLogger::OUT_STDOUT),
44     m_logFileName("ocsd_trace_decode.log"),
45         m_pOutStrI(0)
46 {
47 }
48
49 ocsdMsgLogger::~ocsdMsgLogger()
50 {
51     m_out_file.close();
52 }
53
54 void ocsdMsgLogger::setLogOpts(int logOpts)
55 {
56     m_outFlags = logOpts & (MSGLOG_OUT_MASK);
57 }
58
59 void ocsdMsgLogger::setLogFileName(const char *fileName)
60 {
61     if (fileName == 0)
62         m_logFileName = "";
63     else
64         m_logFileName = fileName;
65
66     if(m_out_file.is_open())
67         m_out_file.close();
68
69     if (m_logFileName.length())
70         m_outFlags |= (int)ocsdMsgLogger::OUT_FILE;
71     else
72         m_outFlags &= ~((int)ocsdMsgLogger::OUT_FILE);
73 }
74
75 void ocsdMsgLogger::setStrOutFn(ocsdMsgLogStrOutI *p_IstrOut)
76 {
77         m_pOutStrI = p_IstrOut;
78     if (p_IstrOut)
79         m_outFlags |= (int)ocsdMsgLogger::OUT_STR_CB;
80     else
81         m_outFlags &= ~((int)ocsdMsgLogger::OUT_STR_CB);
82 }
83
84 void ocsdMsgLogger::LogMsg(const std::string &msg)
85 {
86     if(m_outFlags & OUT_STDOUT)
87     {
88         std::cout << msg;
89         std::cout.flush();
90     }
91
92     if(m_outFlags & OUT_STDERR)
93     {
94         std::cerr << msg;
95         std::cerr.flush();
96     }
97
98     if(m_outFlags & OUT_FILE)
99     {
100         if(!m_out_file.is_open())
101         {
102             m_out_file.open(m_logFileName.c_str(),std::fstream::out | std::fstream::app);
103         }
104         m_out_file << msg;
105         m_out_file.flush();
106     }
107
108         if (m_outFlags & OUT_STR_CB)
109         {
110                 if (m_pOutStrI)
111                         m_pOutStrI->printOutStr(msg);
112         }
113 }
114
115 const bool ocsdMsgLogger::isLogging() const
116 {
117     return (bool)((m_outFlags & MSGLOG_OUT_MASK) != 0);
118 }
119
120 /* End of File ocsd_msg_logger.cpp */