KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DIS_Logger_Record.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: DIS_Logger_Record
32  created: 06:12:2008
33  author: Karl Jones
34 
35  purpose: This class will record DIS data to a file.
36  Each PDU can have additional data attached such as
37  a timestamp which can be used for playing the data
38  back using DIS_Logger_Playback.
39  The files are saved as simple ASCII text files with each
40  PDUs octets written in hex for easy debugging(well easy if you read binary!).
41 
42  Note: You could actually use this class to record any
43  type of network data.
44 *********************************************************************/
45 
46 #pragma once
47 
48 #include <memory>
49 #include <fstream>
50 #include <time.h>
51 #include "./../PDU/Header.h"
52 #include <vector>
53 
54 namespace KDIS {
55 namespace UTILS {
56 
58 {
59 protected:
60 
61  std::fstream m_File;
62 
64 
65  std::vector<KString> m_vsLog;
66 
67  //************************************
68  // FullName: KDIS::UTILS::DIS_Logger_Record::writeToFile
69  //!Description: Saves to the file as text.
70  // Parameter: const KString & S
71  //************************************
72  void writeToFile( const KString & S ) throw( KException );
73 
74  //************************************
75  // FullName: KDIS::UTILS::DIS_Logger_Record::writeToBuffer
76  //!Description: Saves to the buffer.
77  // Parameter: const KString & S
78  //************************************
79  void writeToBuffer( const KString & S );
80 
81 public:
82 
83  // WriteToFile - if true each logged PDU will be written straight
84  // to the file, if false the recorded data is stored untill Save() is
85  // called.
86  DIS_Logger_Record( const KString & FileName, KBOOL WriteToFile );
87 
89 
90  //************************************
91  // FullName: KDIS::UTILS::DIS_Logger_Record::Record
92  //!Description: Record a PDU to the log, Stamp
93  //! can be used for playback (timestamp)
94  //! or simply to add comments to each PDU etc,
95  //! Stamp will be written above each PDU in the file.
96  // Parameter: Type Stamp
97  // Parameter: const KDataStream & Stream
98  //************************************
99  template<class Type>
100  void Record( Type Stamp, const KDataStream & stream ) throw( KException );
101 
102  //************************************
103  // FullName: KDIS::UTILS::DIS_Logger_Record::Save
104  //!Description: If we are not writing each logged
105  //! PDU straight to file this will write
106  //! the buffered data to the file and clear
107  //! the buffer.
108  //************************************
109  void Save() throw( KException );
110 
111  //************************************
112  // FullName: KDIS::UTILS::DIS_Logger_Record::Save
113  //!Description: If we are not writing each logged
114  //! PDU straight to file this will clear
115  //! the buffered data.
116  //************************************
117  void Clear();
118 
119  //************************************
120  // FullName: KDIS::UTILS::DIS_Logger_Record::GetBufferSize
121  //!Description: Returns number of entries in the buffer if we are
122  //! buffering data instead of writing straight
123  //! to the file.
124  //************************************
125  KUINT16 GetBufferSize() const;
126 };
127 
128 //////////////////////////////////////////////////////////////////////////
129 // Template Operators
130 //////////////////////////////////////////////////////////////////////////
131 
132 template<class Type>
133 void DIS_Logger_Record::Record( Type Stamp, const KDataStream & Stream ) throw( KException )
134 {
135  KStringStream ss;
136  ss << Stamp << "\n" << Stream.GetAsString();
137  m_bWriteToFile ? writeToFile( ss.str() ) : writeToBuffer( ss.str() );
138 }
139 
140 //////////////////////////////////////////////////////////////////////////
141 
142 } // END namespace UTILS
143 } // END namespace KDIS
144 
std::fstream m_File
Definition: DIS_Logger_Record.h:61
std::vector< KString > m_vsLog
Definition: DIS_Logger_Record.h:65
unsigned short int KUINT16
Definition: KDefines.h:101
KBOOL m_bWriteToFile
Definition: DIS_Logger_Record.h:63
Definition: EnumEntityInfoInteraction.h:773
Definition: KDefines.h:182
Definition: KDataStream.h:48
bool KBOOL
Definition: KDefines.h:119
Definition: DIS_Logger_Record.h:57
std::string KString
Definition: KDefines.h:116
std::stringstream KStringStream
Definition: KDefines.h:117
#define KDIS_EXPORT
Definition: KDefines.h:82