AGX Dynamics  2.37.3.2
LogStream.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_LOGSTREAM_H
18 #define AGX_LOGSTREAM_H
19 
20 #ifdef _MSC_VER
21 # pragma warning(push)
22 # pragma warning(disable: 4355) // No warnings about using this in member initializer list
23 # pragma warning(disable: 4251) // warning C4251: class X needs to have dll-interface to be used by clients of class Y
24 #endif
25 
26 #include <agx/agxCore_export.h>
27 
28 #include <time.h>
29 
30 #include <iostream>
31 #include <string>
32 #include <sstream>
33 #include <streambuf>
34 #include <agx/String.h>
35 
36 
37 
38 namespace agx
39 {
40 
44  class AGXCORE_EXPORT LogStream : public std::ostream
45  {
46  public:
47 
49  LogStream();
50 
51  public:
52 
53  typedef void ( *CallbackFunction_t )( const char*, unsigned int );
54 
60  static agx::String getTimeString( const agx::String& dateFormat="", bool logTimeWithMilliseconds=true );
61 
67  void setDateFormat( const agx::String& date_format );
68 
70  void setEnableDate( bool flag );
71 
72  bool getEnableDate() const;
73 
77  void setEnableThreadId(bool flag);
78 
79  bool getEnableThreadId() const;
80 
82  const agx::String& getDateFormat() const;
83 
85  enum OutputMode { CERR = 1, COUT = 2, FILE = 4, FUNCTION = 8 };
86 
88  void appendOutputMode( short modemask );
89 
91  void setOutputMode( short modemask );
92 
94  void clearOutputMode( void );
95 
97  int getOutputMode( void );
98 
100  bool isActive( OutputMode modemask );
101 
103  bool open( const agx::String& name );
104 
105  void close( void );
106 
108  bool haveOpenFile() const;
109 
110  agx::String getFilename() const;
111 
112  void flush();
113 
118  virtual ~LogStream();
119 
121  void setCallback( CallbackFunction_t f );
122 
123  //std::ofstream& getStream();
124 
125  CallbackFunction_t getCallback();
126 
131  virtual void message( const char* msg, unsigned int length );
132 
133  protected:
134 
135  size_t write( const char *msg, std::streamsize count);
136 
137  CallbackFunction_t m_callback;
138 
139  int m_mode;
140  ::FILE *m_file;
141  bool m_isOpen;
143 
144  // Copy & assignment are undefined in iostreams
145 #ifndef SWIG
146  LogStream( const LogStream& );
147  LogStream& operator=( const LogStream& );
148 #endif
149 
150  private:
151 
152  agx::String m_date_format;
153  bool m_date_enabled;
154  bool m_threadId_enabled;
155 
156 
158 public:
159  template < class logStreamT, class charT, class traits = std::char_traits<charT> >
160  class AGXCORE_EXPORT LogBuf : public std::basic_streambuf<charT, traits>
161  {
162  public:
163 
164  LogBuf( logStreamT* stream ) : m_stream( stream ) {}
165  virtual ~LogBuf(){};
166 
167  protected:
168 
169  typedef LogBuf<logStreamT, charT> classT;
170  typedef std::basic_streambuf<charT, traits> BaseClassT;
171 
172  std::streamsize output( const char* pStr, std::streamsize n );
173 
174  int overflow ( int ch );
175 
176  int sync ();
177 
178  LogBuf( const LogBuf& );
179  LogBuf& operator=( const LogBuf& );
180  logStreamT* m_stream;
181  std::ostringstream m_strStream;
182 
183  };
184 
185  typedef LogBuf<LogStream, char> LogBufT;
186  LogBufT* m_logBuf;
187 
188  template < class logStreamT, class charT, class traits > friend class LogBuf;
190 
191  };
192 } // namespace agx
193 
194 #ifdef _MSC_VER
195 # pragma warning(pop)
196 #endif
197 
198 #endif
Class for printing out messages to console, file, virtual method and or a callback function...
Definition: LogStream.h:44
OutputMode
Enums used to specify the output mode used. These can be OR:ed together.
Definition: LogStream.h:85
STL namespace.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
bool AGXPHYSICS_EXPORT write(const std::string &filename, const agxStream::Serializable *data)
Write a Serializable object to a file.
agx::String m_filename
Definition: LogStream.h:142
CallbackFunction_t m_callback
Definition: LogStream.h:137
::FILE * m_file
Definition: LogStream.h:140
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97