AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
LogStream.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
38namespace agx
39{
40
44 class AGXCORE_EXPORT LogStream : public std::ostream
45 {
46 public:
47
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
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
140 ::FILE *m_file;
143
144 // Copy & assignment are undefined in iostreams
145#ifndef SWIG
148#endif
149
150 private:
151
152 agx::String m_date_format;
153 bool m_date_enabled;
154 bool m_threadId_enabled;
155
156
158public:
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
#define AGXCORE_EXPORT
Class for printing out messages to console, file, virtual method and or a callback function.
Definition: LogStream.h:45
void setEnableDate(bool flag)
If flag == true, the date in the specified date format will be added prior to each message.
void appendOutputMode(short modemask)
OR:s together the active outputmode with modemask.
void setOutputMode(short modemask)
Set the active outputmode to modemask.
void setCallback(CallbackFunction_t f)
Sets the function that will be called if FUNCTION is set as outputmode.
const agx::String & getDateFormat() const
::FILE * m_file
Definition: LogStream.h:140
bool getEnableDate() const
virtual ~LogStream()
Flush and empty all buffers, and then remove the allocated buffers.
LogStream & operator=(const LogStream &)
void close(void)
void clearOutputMode(void)
Resets the active output modes to none.
agx::String getFilename() const
LogStream(const LogStream &)
LogStream()
Constructor.
void setEnableThreadId(bool flag)
If set to true, the messages will be prefixed with the id of the thread that generates the message.
CallbackFunction_t m_callback
Definition: LogStream.h:137
OutputMode
Enums used to specify the output mode used. These can be OR:ed together.
Definition: LogStream.h:85
static agx::String getTimeString(const agx::String &dateFormat="", bool logTimeWithMilliseconds=true)
bool isActive(OutputMode modemask)
Returns true if the modemask is set as the active output mode.
void setDateFormat(const agx::String &date_format)
Specify the format of the date/time string that getTimeString() will return Look at strftime for exam...
agx::String m_filename
Definition: LogStream.h:142
CallbackFunction_t getCallback()
virtual void message(const char *msg, unsigned int length)
This method will be called whenever there is a message ready for output.
bool open(const agx::String &name)
Open a specified file as a log.
size_t write(const char *msg, std::streamsize count)
int getOutputMode(void)
Returns the active outputmode.
bool haveOpenFile() const
bool getEnableThreadId() const
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
STL namespace.