FileLogger.cpp
Go to the documentation of this file.
00001 /*=============================================================================
00002   Copyright (C) 2012 Allied Vision Technologies.  All Rights Reserved.
00003 
00004   Redistribution of this file, in original or modified form, without
00005   prior written consent of Allied Vision Technologies is prohibited.
00006 
00007 -------------------------------------------------------------------------------
00008 
00009   File:        FileLogger.cpp
00010 
00011   Description: Implementation of class AVT::VmbAPI::FileLogger.
00012 
00013 -------------------------------------------------------------------------------
00014 
00015   THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
00016   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
00017   NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR  PURPOSE ARE
00018   DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
00019   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00020   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00021   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  
00022   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
00023   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00024   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026 =============================================================================*/
00027 
00028 #include <ctime>
00029 #include <cstdlib>
00030 
00031 #include <VimbaCPP/Include/FileLogger.h>
00032 #include <VimbaCPP/Source/MutexGuard.h>
00033 
00034 #ifdef _WIN32
00035 #pragma warning(disable: 4996)
00036 #else //_WIN32
00037 #include <sys/stat.h>
00038 #endif //_WIN32
00039 
00040 namespace AVT {
00041 namespace VmbAPI {
00042 
00043 FileLogger::FileLogger( const char *pFileName, bool bAppend )
00044     :   m_pMutex( MutexPtr( new Mutex() ))
00045 {
00046     std::string strTempPath = GetTempPath();
00047     std::string strFileName( pFileName );
00048 
00049     if ( 0 < strTempPath.length() )
00050     {
00051         strFileName = strTempPath.append( strFileName );
00052         if( true == bAppend )
00053         {
00054             m_File.open( strFileName.c_str(), std::fstream::app );
00055         }
00056         else
00057         {
00058             m_File.open( strFileName.c_str() );
00059         }
00060     }
00061     else
00062     {
00063         throw;
00064     }
00065 }
00066 
00067 FileLogger::FileLogger( const FileLogger& )
00068 {
00069     // No copy ctor
00070 }
00071 
00072 FileLogger& FileLogger::operator=( const FileLogger& )
00073 {
00074     // No assignment operator
00075     return *this;
00076 }
00077 
00078 FileLogger::~FileLogger()
00079 {
00080     if( true == m_File.is_open() )
00081     {
00082         m_File.close();
00083     }
00084 }
00085 
00086 void FileLogger::Log( const std::string &rStrMessage )
00087 {
00088     MutexGuard guard( m_pMutex );
00089 
00090     if( true == m_File.is_open() )
00091     {
00092         #ifdef _WIN32
00093             time_t nTime = time( &nTime );
00094             tm timeInfo;
00095             localtime_s( &timeInfo, &nTime );
00096             char strTime[100];
00097             asctime_s( strTime, 100, &timeInfo );
00098         #else
00099             time_t nTime = time( NULL );
00100             std::string strTime = asctime( localtime( &nTime ) );
00101         #endif
00102 
00103         m_File << strTime << ": " << rStrMessage << std::endl;
00104         m_File.flush();
00105     }
00106 }
00107 
00108 std::string FileLogger::GetTempPath()
00109 {
00110 #ifndef _WIN32
00111     std::string tmpDir;
00112     
00113     if(tmpDir.size() == 0)
00114     {
00115         char *pPath = std::getenv("TMPDIR");
00116         if(NULL != pPath)
00117         {
00118             struct stat lStats;
00119             if(stat(pPath, &lStats) == 0)
00120             {
00121                 tmpDir = pPath;
00122             }
00123         }
00124     }
00125     if(tmpDir.size() == 0)
00126     {
00127         char *pPath = std::getenv("TEMP");
00128         if(NULL != pPath)
00129         {
00130             struct stat lStats;
00131             if(stat(pPath, &lStats) == 0)
00132             {
00133                 tmpDir = pPath;
00134             }
00135         }
00136     }
00137     if(tmpDir.size() == 0)
00138     {
00139         char *pPath = std::getenv("TMP");
00140         if(NULL != pPath)
00141         {
00142             struct stat lStats;
00143             if(stat(pPath, &lStats) == 0)
00144             {
00145                 tmpDir = pPath;
00146             }
00147         }
00148     }
00149     if(tmpDir.size() == 0)
00150     {
00151         std::string path = "/tmp";
00152         struct stat lStats;
00153         if(stat(path.c_str(), &lStats) == 0)
00154         {
00155             tmpDir = path;
00156         }
00157     }
00158     if(tmpDir.size() == 0)
00159     {
00160         std::string path = "/var/tmp";
00161         struct stat lStats;
00162         if(stat(path.c_str(), &lStats) == 0)
00163         {
00164             tmpDir = path;
00165         }
00166     }
00167     if(tmpDir.size() == 0)
00168     {
00169         std::string path = "/usr/tmp";
00170         struct stat lStats;
00171         if(stat(path.c_str(), &lStats) == 0)
00172         {
00173             tmpDir = path;
00174         }
00175     }
00176     if(tmpDir.size() == 0)
00177     {
00178         return "";
00179     }
00180     // everyone expects delimiter on the outside
00181     if( (*tmpDir.rbegin()) != '/' )
00182     {
00183         tmpDir +='/';
00184     }
00185     return tmpDir;
00186 #else
00187     DWORD length = ::GetTempPathA( 0, NULL );
00188     if( length == 0 )
00189     {
00190         return "";
00191     }
00192     
00193     std::vector<TCHAR> tempPath( length );
00194 
00195     length = ::GetTempPath( static_cast<DWORD>( tempPath.size() ), &tempPath[0] );
00196     if( length == 0 || length > tempPath.size() )
00197     {
00198         return "";
00199     }
00200 
00201     return std::string( tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(length) );
00202 #endif
00203 }
00204 
00205 }} //namespace AV::VmbAPI


avt_vimba_camera
Author(s): Miquel Massot , Allied Vision Technologies
autogenerated on Thu Jun 6 2019 18:23:39