FileLogger.cpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved.
3 
4  Redistribution of this file, in original or modified form, without
5  prior written consent of Allied Vision Technologies is prohibited.
6 
7 -------------------------------------------------------------------------------
8 
9  File: FileLogger.cpp
10 
11  Description: Implementation of class AVT::VmbAPI::FileLogger.
12 
13 -------------------------------------------------------------------------------
14 
15  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
17  NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 =============================================================================*/
27 
28 #include <ctime>
29 #include <cstdlib>
30 
33 
34 #ifdef _WIN32
35 #pragma warning(disable: 4996)
36 #else //_WIN32
37 #include <sys/stat.h>
38 #endif //_WIN32
39 
40 namespace AVT {
41 namespace VmbAPI {
42 
43 FileLogger::FileLogger( const char *pFileName, bool bAppend )
44  : m_pMutex( MutexPtr( new Mutex() ))
45 {
46  std::string strTempPath = GetTempPath();
47  std::string strFileName( pFileName );
48 
49  if ( 0 < strTempPath.length() )
50  {
51  strFileName = strTempPath.append( strFileName );
52  if( true == bAppend )
53  {
54  m_File.open( strFileName.c_str(), std::fstream::app );
55  }
56  else
57  {
58  m_File.open( strFileName.c_str() );
59  }
60  }
61  else
62  {
63  throw;
64  }
65 }
66 
68 {
69  // No copy ctor
70 }
71 
73 {
74  // No assignment operator
75  return *this;
76 }
77 
79 {
80  if( true == m_File.is_open() )
81  {
82  m_File.close();
83  }
84 }
85 
86 void FileLogger::Log( const std::string &rStrMessage )
87 {
88  MutexGuard guard( m_pMutex );
89 
90  if( true == m_File.is_open() )
91  {
92  #ifdef _WIN32
93  time_t nTime = time( &nTime );
94  tm timeInfo;
95  localtime_s( &timeInfo, &nTime );
96  char strTime[100];
97  asctime_s( strTime, 100, &timeInfo );
98  #else
99  time_t nTime = time( NULL );
100  std::string strTime = asctime( localtime( &nTime ) );
101  #endif
102 
103  m_File << strTime << ": " << rStrMessage << std::endl;
104  m_File.flush();
105  }
106 }
107 
109 {
110 #ifndef _WIN32
111  std::string tmpDir;
112 
113  if(tmpDir.size() == 0)
114  {
115  char *pPath = std::getenv("TMPDIR");
116  if(NULL != pPath)
117  {
118  struct stat lStats;
119  if(stat(pPath, &lStats) == 0)
120  {
121  tmpDir = pPath;
122  }
123  }
124  }
125  if(tmpDir.size() == 0)
126  {
127  char *pPath = std::getenv("TEMP");
128  if(NULL != pPath)
129  {
130  struct stat lStats;
131  if(stat(pPath, &lStats) == 0)
132  {
133  tmpDir = pPath;
134  }
135  }
136  }
137  if(tmpDir.size() == 0)
138  {
139  char *pPath = std::getenv("TMP");
140  if(NULL != pPath)
141  {
142  struct stat lStats;
143  if(stat(pPath, &lStats) == 0)
144  {
145  tmpDir = pPath;
146  }
147  }
148  }
149  if(tmpDir.size() == 0)
150  {
151  std::string path = "/tmp";
152  struct stat lStats;
153  if(stat(path.c_str(), &lStats) == 0)
154  {
155  tmpDir = path;
156  }
157  }
158  if(tmpDir.size() == 0)
159  {
160  std::string path = "/var/tmp";
161  struct stat lStats;
162  if(stat(path.c_str(), &lStats) == 0)
163  {
164  tmpDir = path;
165  }
166  }
167  if(tmpDir.size() == 0)
168  {
169  std::string path = "/usr/tmp";
170  struct stat lStats;
171  if(stat(path.c_str(), &lStats) == 0)
172  {
173  tmpDir = path;
174  }
175  }
176  if(tmpDir.size() == 0)
177  {
178  return "";
179  }
180  // everyone expects delimiter on the outside
181  if( (*tmpDir.rbegin()) != '/' )
182  {
183  tmpDir +='/';
184  }
185  return tmpDir;
186 #else
187  DWORD length = ::GetTempPathA( 0, NULL );
188  if( length == 0 )
189  {
190  return "";
191  }
192 
193  std::vector<TCHAR> tempPath( length );
194 
195  length = ::GetTempPath( static_cast<DWORD>( tempPath.size() ), &tempPath[0] );
196  if( length == 0 || length > tempPath.size() )
197  {
198  return "";
199  }
200 
201  return std::string( tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(length) );
202 #endif
203 }
204 
205 }} //namespace AV::VmbAPI
FileLogger(const char *pFileName, bool append=true)
Definition: FileLogger.cpp:43
std::string GetTempPath()
Definition: FileLogger.cpp:108
FileLogger & operator=(const FileLogger &)
Definition: FileLogger.cpp:72
void Log(const std::string &StrMessage)
Definition: FileLogger.cpp:86
std::ofstream m_File
Definition: FileLogger.h:50


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Mon Jun 10 2019 12:50:39