2009-09-08 12:08:10 +00:00
/* PCSX2 - PS2 Emulator for PCs
2010-05-03 14:08:02 +00:00
* Copyright ( C ) 2002 - 2010 PCSX2 Dev Team
2009-10-04 09:00:07 +00:00
*
2009-09-08 12:08:10 +00:00
* PCSX2 is free software : you can redistribute it and / or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found -
* ation , either version 3 of the License , or ( at your option ) any later version .
2009-04-27 02:04:31 +00:00
*
2009-09-08 12:08:10 +00:00
* PCSX2 is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE . See the GNU General Public License for more details .
2009-04-28 05:56:22 +00:00
*
2009-09-08 12:08:10 +00:00
* You should have received a copy of the GNU General Public License along with PCSX2 .
* If not , see < http : //www.gnu.org/licenses/>.
2009-04-27 02:04:31 +00:00
*/
2022-05-18 13:27:23 +00:00
# include "Threading.h"
# include "General.h"
# include "Exceptions.h"
2021-09-03 10:43:33 +00:00
2022-05-18 13:27:23 +00:00
# include "fmt/core.h"
2009-12-25 18:58:25 +00:00
2022-05-18 13:27:23 +00:00
# ifdef _WIN32
# include "RedtapeWindows.h"
# endif
2021-09-01 20:31:46 +00:00
2022-05-18 13:27:23 +00:00
# ifdef __UNIX__
# include <signal.h>
# endif
2010-11-05 01:33:01 +00:00
2009-09-12 02:58:22 +00:00
// ------------------------------------------------------------------------
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
// Force DevAssert to *not* inline for devel builds (allows using breakpoints to trap assertions,
// and force it to inline for release builds (optimizes it out completely since IsDevBuild is a
// const false).
2009-09-12 02:58:22 +00:00
//
# ifdef PCSX2_DEVBUILD
2016-11-12 15:28:37 +00:00
# define DEVASSERT_INLINE __noinline
2009-09-12 02:58:22 +00:00
# else
2016-11-12 15:28:37 +00:00
# define DEVASSERT_INLINE __fi
2009-09-12 02:58:22 +00:00
# endif
2021-09-06 18:28:26 +00:00
pxDoAssertFnType * pxDoAssert = pxAssertImpl_LogIt ;
2009-12-14 12:18:55 +00:00
// make life easier for people using VC++ IDE by using this format, which allows double-click
// response times from the Output window...
2022-05-18 13:27:23 +00:00
std : : string DiagnosticOrigin : : ToString ( const char * msg ) const
2009-12-14 12:18:55 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string message ;
2009-12-14 12:18:55 +00:00
2022-05-18 13:27:23 +00:00
fmt : : format_to ( std : : back_inserter ( message ) , " {}({}) : assertion failed: \n " , srcfile , line ) ;
2009-12-14 12:18:55 +00:00
2022-05-18 13:27:23 +00:00
if ( function )
fmt : : format_to ( std : : back_inserter ( message ) , " Function: {} \n " , function ) ;
2009-12-14 12:18:55 +00:00
2022-05-18 13:27:23 +00:00
if ( condition )
fmt : : format_to ( std : : back_inserter ( message ) , " Condition: {} \n " , condition ) ;
2009-12-14 12:18:55 +00:00
2022-05-18 13:27:23 +00:00
if ( msg )
fmt : : format_to ( std : : back_inserter ( message ) , " Message: {} \n " , msg ) ;
2009-12-14 12:18:55 +00:00
2021-09-06 18:28:26 +00:00
return message ;
2009-12-14 12:18:55 +00:00
}
2009-12-25 18:00:51 +00:00
// Because wxTrap isn't available on Linux builds of wxWidgets (non-Debug, typically)
void pxTrap ( )
{
2022-05-18 13:27:23 +00:00
# if defined(_WIN32)
2021-09-06 18:28:26 +00:00
__debugbreak ( ) ;
2009-12-25 18:00:51 +00:00
# elif defined(__UNIX__)
2021-09-06 18:28:26 +00:00
raise ( SIGTRAP ) ;
2009-12-25 18:00:51 +00:00
# else
2022-05-18 13:27:23 +00:00
abort ( ) ;
# endif
2009-12-25 18:00:51 +00:00
}
2010-10-22 19:47:02 +00:00
2022-05-18 13:27:23 +00:00
bool pxAssertImpl_LogIt ( const DiagnosticOrigin & origin , const char * msg )
2010-10-22 19:47:02 +00:00
{
2021-09-06 18:28:26 +00:00
//wxLogError( L"%s", origin.ToString( msg ).c_str() );
2022-05-18 13:27:23 +00:00
std : : string full_msg ( origin . ToString ( msg ) ) ;
# ifdef _WIN32
OutputDebugStringA ( full_msg . c_str ( ) ) ;
OutputDebugStringA ( " \n " ) ;
# endif
std : : fprintf ( stderr , " %s \n " , full_msg . c_str ( ) ) ;
2021-09-06 18:28:26 +00:00
pxTrap ( ) ;
return false ;
2010-10-22 19:47:02 +00:00
}
2022-05-18 13:27:23 +00:00
DEVASSERT_INLINE void pxOnAssert ( const DiagnosticOrigin & origin , const char * msg )
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
{
2021-09-06 18:28:26 +00:00
// wxWidgets doesn't come with debug builds on some Linux distros, and other distros make
// it difficult to use the debug build (compilation failures). To handle these I've had to
// bypass the internal wxWidgets assertion handler entirely, since it may not exist even if
// PCSX2 itself is compiled in debug mode (assertions enabled).
2009-12-14 12:18:55 +00:00
2021-09-06 18:28:26 +00:00
bool trapit ;
2009-12-14 12:18:55 +00:00
2021-09-06 18:28:26 +00:00
if ( pxDoAssert = = NULL )
{
// Note: Format uses MSVC's syntax for output window hotlinking.
2022-05-18 13:27:23 +00:00
trapit = pxAssertImpl_LogIt ( origin , msg ) ;
2021-09-06 18:28:26 +00:00
}
else
{
2022-05-18 13:27:23 +00:00
trapit = pxDoAssert ( origin , msg ) ;
2021-09-06 18:28:26 +00:00
}
2009-12-14 12:18:55 +00:00
2021-09-06 18:28:26 +00:00
if ( trapit )
{
pxTrap ( ) ;
}
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
}
2009-09-23 09:53:21 +00:00
2009-09-20 20:54:45 +00:00
// --------------------------------------------------------------------------------------
2010-06-28 18:03:54 +00:00
// BaseException (implementations)
2009-09-20 20:54:45 +00:00
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
BaseException & BaseException : : SetBothMsgs ( const char * msg_diag )
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
m_message_user = msg_diag ? std : : string ( msg_diag ) : std : : string ( ) ;
2021-09-06 18:28:26 +00:00
return SetDiagMsg ( msg_diag ) ;
2009-09-20 20:54:45 +00:00
}
2009-04-28 05:56:22 +00:00
2022-05-18 13:27:23 +00:00
BaseException & BaseException : : SetDiagMsg ( std : : string msg_diag )
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
m_message_diag = std : : move ( msg_diag ) ;
2021-09-06 18:28:26 +00:00
return * this ;
2010-06-28 18:03:54 +00:00
}
2009-09-01 00:43:28 +00:00
2022-05-18 13:27:23 +00:00
BaseException & BaseException : : SetUserMsg ( std : : string msg_user )
2010-06-28 18:03:54 +00:00
{
2022-05-18 13:27:23 +00:00
m_message_user = std : : move ( msg_user ) ;
2021-09-06 18:28:26 +00:00
return * this ;
2009-09-20 20:54:45 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string BaseException : : FormatDiagnosticMessage ( ) const
2009-09-20 20:54:45 +00:00
{
2021-09-06 18:28:26 +00:00
return m_message_diag ;
2009-09-20 20:54:45 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string BaseException : : FormatDisplayMessage ( ) const
2010-06-28 18:03:54 +00:00
{
2022-05-18 13:27:23 +00:00
return m_message_user . empty ( ) ? m_message_diag : m_message_user ;
2010-06-28 18:03:54 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::RuntimeError (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
Exception : : RuntimeError : : RuntimeError ( const std : : runtime_error & ex , const char * prefix /* = nullptr */ )
2010-04-27 13:12:03 +00:00
{
2021-09-06 18:28:26 +00:00
IsSilent = false ;
2010-06-28 18:03:54 +00:00
2022-05-18 13:27:23 +00:00
const bool has_prefix = prefix & & prefix [ 0 ] ! = 0 ;
SetDiagMsg ( fmt : : format ( " STL Runtime Error{}{}{}: {} " ,
has_prefix ? " ( " : " " , prefix ? prefix : " " , has_prefix ? " ) " : " " ,
ex . what ( ) ) ) ;
2010-05-07 03:20:58 +00:00
}
2022-05-18 13:27:23 +00:00
Exception : : RuntimeError : : RuntimeError ( const std : : exception & ex , const char * prefix /* = nullptr */ )
2010-05-07 03:20:58 +00:00
{
2021-09-06 18:28:26 +00:00
IsSilent = false ;
2010-06-28 18:03:54 +00:00
2022-05-18 13:27:23 +00:00
const bool has_prefix = prefix & & prefix [ 0 ] ! = 0 ;
SetDiagMsg ( fmt : : format ( " STL Exception{}{}{}: {} " ,
has_prefix ? " ( " : " " , prefix ? prefix : " " , has_prefix ? " ) " : " " ,
ex . what ( ) ) ) ;
2010-04-27 13:12:03 +00:00
}
2010-06-28 18:03:54 +00:00
// --------------------------------------------------------------------------------------
// Exception::OutOfMemory (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
Exception : : OutOfMemory : : OutOfMemory ( std : : string allocdesc )
: AllocDescription ( std : : move ( allocdesc ) )
2009-10-07 19:20:11 +00:00
{
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : OutOfMemory : : FormatDiagnosticMessage ( ) const
2009-10-07 19:20:11 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retmsg ;
retmsg = " Out of memory " ;
if ( ! AllocDescription . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " while allocating '{}' " , AllocDescription ) ;
2010-10-29 02:49:01 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_diag . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " : \n {} " , m_message_diag ) ;
2016-11-12 15:28:37 +00:00
2021-09-06 18:28:26 +00:00
return retmsg ;
2009-10-07 19:20:11 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : OutOfMemory : : FormatDisplayMessage ( ) const
2010-06-28 18:03:54 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retmsg ;
retmsg = " Oh noes! Out of memory! " ;
2010-11-05 01:33:01 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_user . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " \n \n {} " , m_message_user ) ;
2010-11-05 01:33:01 +00:00
2021-09-06 18:28:26 +00:00
return retmsg ;
2010-10-29 02:49:01 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::VirtualMemoryMapConflict (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
Exception : : VirtualMemoryMapConflict : : VirtualMemoryMapConflict ( std : : string allocdesc )
2010-10-29 02:49:01 +00:00
{
2022-05-18 13:27:23 +00:00
AllocDescription = std : : move ( allocdesc ) ;
m_message_user = " Virtual memory mapping failure! Your system may have conflicting device drivers, services, or may simply have insufficient memory or resources to meet PCSX2's lofty needs. " ;
2010-10-29 02:49:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : VirtualMemoryMapConflict : : FormatDiagnosticMessage ( ) const
2010-10-29 02:49:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retmsg ;
retmsg = " Virtual memory map failed " ;
if ( ! AllocDescription . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " while reserving '{}' " , AllocDescription ) ;
2016-11-12 15:28:37 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_diag . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " : \n {} " , m_message_diag ) ;
2010-10-29 02:49:01 +00:00
2021-09-06 18:28:26 +00:00
return retmsg ;
2010-10-29 02:49:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : VirtualMemoryMapConflict : : FormatDisplayMessage ( ) const
2010-10-29 02:49:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retmsg ;
retmsg = " There is not enough virtual memory available, or necessary virtual memory mappings have already been reserved by other processes, services, or DLLs. " ;
2010-11-05 01:33:01 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_diag . empty ( ) )
fmt : : format_to ( std : : back_inserter ( retmsg ) , " \n \n {} " , m_message_diag ) ;
2010-11-05 01:33:01 +00:00
2021-09-06 18:28:26 +00:00
return retmsg ;
2010-06-28 18:03:54 +00:00
}
2009-09-20 20:54:45 +00:00
// ------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : CancelEvent : : FormatDiagnosticMessage ( ) const
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
return " Action canceled: " + m_message_diag ;
2009-09-20 20:54:45 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : CancelEvent : : FormatDisplayMessage ( ) const
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
return " Action canceled: " + m_message_diag ;
2009-09-20 20:54:45 +00:00
}
2010-11-05 01:33:01 +00:00
// --------------------------------------------------------------------------------------
// Exception::BadStream (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : BadStream : : FormatDiagnosticMessage ( ) const
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
2021-09-06 18:28:26 +00:00
_formatDiagMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : BadStream : : FormatDisplayMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
2021-09-06 18:28:26 +00:00
_formatUserMsg ( retval ) ;
return retval ;
2009-09-20 20:54:45 +00:00
}
2022-05-18 13:27:23 +00:00
void Exception : : BadStream : : _formatDiagMsg ( std : : string & dest ) const
2009-09-20 20:54:45 +00:00
{
2022-05-18 13:27:23 +00:00
fmt : : format_to ( std : : back_inserter ( dest ) , " Path: " ) ;
if ( ! StreamName . empty ( ) )
fmt : : format_to ( std : : back_inserter ( dest ) , " {} " , StreamName ) ;
2021-09-06 18:28:26 +00:00
else
2022-05-18 13:27:23 +00:00
dest + = " [Unnamed or unknown] " ;
2010-06-28 18:03:54 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_diag . empty ( ) )
fmt : : format_to ( std : : back_inserter ( dest ) , " \n {} " , m_message_diag ) ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
void Exception : : BadStream : : _formatUserMsg ( std : : string & dest ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
fmt : : format_to ( std : : back_inserter ( dest ) , " Path: " ) ;
if ( ! StreamName . empty ( ) )
fmt : : format_to ( std : : back_inserter ( dest ) , " {} " , StreamName ) ;
2021-09-06 18:28:26 +00:00
else
2022-05-18 13:27:23 +00:00
dest + = " [Unnamed or unknown] " ;
2010-11-05 01:33:01 +00:00
2022-05-18 13:27:23 +00:00
if ( ! m_message_user . empty ( ) )
fmt : : format_to ( std : : back_inserter ( dest ) , " \n {} " , m_message_user ) ;
2010-11-05 01:33:01 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::CannotCreateStream (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : CannotCreateStream : : FormatDiagnosticMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " File could not be created. " ;
2021-09-06 18:28:26 +00:00
_formatDiagMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : CannotCreateStream : : FormatDisplayMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " A file could not be created. \n " ;
2021-09-06 18:28:26 +00:00
_formatUserMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::FileNotFound (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : FileNotFound : : FormatDiagnosticMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " File not found. \n " ;
2021-09-06 18:28:26 +00:00
_formatDiagMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : FileNotFound : : FormatDisplayMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " File not found. \n " ;
2021-09-06 18:28:26 +00:00
_formatUserMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::AccessDenied (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : AccessDenied : : FormatDiagnosticMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " Permission denied to file. \n " ;
2021-09-06 18:28:26 +00:00
_formatDiagMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : AccessDenied : : FormatDisplayMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " Permission denied while trying to open file, likely due to insufficient user account rights. \n " ;
2021-09-06 18:28:26 +00:00
_formatUserMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
// --------------------------------------------------------------------------------------
// Exception::EndOfStream (implementations)
// --------------------------------------------------------------------------------------
2022-05-18 13:27:23 +00:00
std : : string Exception : : EndOfStream : : FormatDiagnosticMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " Unexpected end of file or stream. \n " ;
2021-09-06 18:28:26 +00:00
_formatDiagMsg ( retval ) ;
return retval ;
2010-11-05 01:33:01 +00:00
}
2022-05-18 13:27:23 +00:00
std : : string Exception : : EndOfStream : : FormatDisplayMessage ( ) const
2010-11-05 01:33:01 +00:00
{
2022-05-18 13:27:23 +00:00
std : : string retval ;
retval = " Unexpected end of file or stream encountered. File is probably truncated or corrupted. \n " ;
2021-09-06 18:28:26 +00:00
_formatUserMsg ( retval ) ;
return retval ;
2009-09-20 20:54:45 +00:00
}
2010-10-18 01:40:49 +00:00
// --------------------------------------------------------------------------------------
// Exceptions from Errno (POSIX)
// --------------------------------------------------------------------------------------
// Translates an Errno code into an exception.
// Throws an exception based on the given error code (usually taken from ANSI C's errno)
2022-05-18 13:27:23 +00:00
BaseException * Exception : : FromErrno ( std : : string streamname , int errcode )
2010-10-18 01:40:49 +00:00
{
2021-09-06 18:28:26 +00:00
pxAssumeDev ( errcode ! = 0 , " Invalid NULL error code? (errno) " ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
switch ( errcode )
{
case EINVAL :
2022-05-18 13:27:23 +00:00
pxFailDev ( " Invalid argument " ) ;
return & ( new Exception : : BadStream ( streamname ) ) - > SetDiagMsg ( " Invalid argument? (likely caused by an unforgivable programmer error!) " ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case EACCES : // Access denied!
return new Exception : : AccessDenied ( streamname ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case EMFILE : // Too many open files!
2022-05-18 13:27:23 +00:00
return & ( new Exception : : CannotCreateStream ( streamname ) ) - > SetDiagMsg ( " Too many open files " ) ; // File handle allocation failure
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case EEXIST :
2022-05-18 13:27:23 +00:00
return & ( new Exception : : CannotCreateStream ( streamname ) ) - > SetDiagMsg ( " File already exists " ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case ENOENT : // File not found!
return new Exception : : FileNotFound ( streamname ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case EPIPE :
2022-05-18 13:27:23 +00:00
return & ( new Exception : : BadStream ( streamname ) ) - > SetDiagMsg ( " Broken pipe " ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
case EBADF :
2022-05-18 13:27:23 +00:00
return & ( new Exception : : BadStream ( streamname ) ) - > SetDiagMsg ( " Bad file number " ) ;
2010-10-18 01:40:49 +00:00
2021-09-06 18:28:26 +00:00
default :
2022-05-18 13:27:23 +00:00
return & ( new Exception : : BadStream ( streamname ) ) - > SetDiagMsg ( fmt : : format ( " General file/stream error [errno: {}] " , errcode ) ) ;
2021-09-06 18:28:26 +00:00
}
2010-10-18 01:40:49 +00:00
}