Torque3D Documentation / _generateds / platformAssert.cpp

platformAssert.cpp

Engine/source/platform/platformAssert.cpp

More...

Public Variables

const char *

Public Functions

const char *
avar(const char * in_msg, ... )
DefineEngineFunction(Assert , void , (bool condition, const char *message) , "Fatal Script Assertion" )

Detailed Description

Public Variables

const char * typeName []

Public Functions

avar(const char * in_msg, ... )

Sprintf style string formating into a fixed temporary buffer. Parameters:

in_msg

sprintf style format string

return:

pointer to fixed buffer containing formatted string

Example:
U8 a = 5;
S16 b = -10;
char *output = avar("hello %s! a=%u, b=%d", "world");
ouput = "hello world! a=5, b=-10"

warning:

avar uses a static fixed buffer. Treat the buffer as volatile data and use it immediately. Other functions my use avar too and clobber the buffer.

DefineEngineFunction(Assert , void , (bool condition, const char *message) , "Fatal Script Assertion" )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include <stdarg.h>
 25
 26#include "core/strings/stringFunctions.h"
 27#include "console/engineAPI.h"
 28
 29
 30//-------------------------------------- STATIC Declaration
 31PlatformAssert *PlatformAssert::platformAssert = NULL;
 32
 33//--------------------------------------
 34PlatformAssert::PlatformAssert()
 35{
 36   processing = false;
 37   ignoreAll = false;
 38}
 39
 40//--------------------------------------
 41PlatformAssert::~PlatformAssert()
 42{
 43}
 44
 45//--------------------------------------
 46void PlatformAssert::create( PlatformAssert* newAssertClass )
 47{
 48   if (!platformAssert)
 49      platformAssert = newAssertClass ? newAssertClass : new PlatformAssert;
 50}
 51
 52
 53//--------------------------------------
 54void PlatformAssert::destroy()
 55{
 56   if (platformAssert)
 57      delete platformAssert;
 58   platformAssert = NULL;
 59}
 60
 61
 62//--------------------------------------
 63bool PlatformAssert::displayMessageBox(const char *title, const char *message, bool retry)
 64{
 65   if (retry)
 66      return Platform::AlertRetry(title, message);
 67
 68   Platform::AlertOK(title, message);
 69   return false;
 70}
 71
 72static const char *typeName[] = { "Unknown", "Fatal-ISV", "Fatal", "Warning" };
 73
 74//--------------------------------------
 75
 76bool PlatformAssert::process(Type         assertType,
 77                             const char  *filename,
 78                             U32          lineNumber,
 79                             const char  *message)
 80{
 81    // If we're somehow recursing, just die.
 82    if(processing)
 83        Platform::debugBreak();
 84    
 85    processing = true;
 86    bool ret = false;
 87    
 88    // always dump to the Assert to the Console
 89    if (Con::isActive())
 90    {
 91        if (assertType == Warning)
 92            Con::warnf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], message);
 93        else
 94            Con::errorf(ConsoleLogEntry::Assert, "%s(%ld,0): {%s} - %s", filename, lineNumber, typeName[assertType], message);
 95    }
 96    
 97    // if not a WARNING pop-up a dialog box
 98    if (assertType != Warning)
 99    {
100        // used for processing navGraphs (an assert won't botch the whole build)
101        if(Con::getBoolVariable("$FP::DisableAsserts", false) == true)
102            Platform::forceShutdown(1);
103        
104        char buffer[2048];
105        dSprintf(buffer, 2048, "%s: (%s @ %ld)", typeName[assertType], filename, lineNumber);
106        if( !ignoreAll )
107        {
108            // Display message box with Debug, Ignore, Ignore All, and Exit options
109            switch( Platform::AlertAssert(buffer, message) )
110            {
111                case Platform::ALERT_ASSERT_DEBUG:          
112                    ret = true;
113                    break;
114                case Platform::ALERT_ASSERT_IGNORE:
115                    ret = false;
116                    break;
117                case Platform::ALERT_ASSERT_IGNORE_ALL:
118                    ignoreAll = true;
119                    ret = false;
120                    break;
121                default:
122                case Platform::ALERT_ASSERT_EXIT:
123                    Platform::forceShutdown(1);
124                    break;
125            }
126        }
127    }
128    
129    processing = false;
130    
131    return ret;
132}
133
134bool PlatformAssert::processingAssert()
135{
136   return platformAssert ? platformAssert->processing : false;
137}
138
139//--------------------------------------
140bool PlatformAssert::processAssert(Type        assertType,
141                                   const char  *filename,
142                                   U32         lineNumber,
143                                   const char  *message)
144{
145   if (platformAssert)
146      return platformAssert->process(assertType, filename, lineNumber, message);
147   else // when platAssert NULL (during _start/_exit) try direct output...
148      dPrintf("\n%s: (%s @ %ld) %s\n", typeName[assertType], filename, lineNumber, message);
149
150   // this could also be platform-specific: OutputDebugString on PC, DebugStr on Mac.
151   // Will raw printfs do the job?  In the worst case, it's a break-pointable line of code.
152   // would have preferred Con but due to race conditions, it might not be around...
153   // Con::errorf(ConsoleLogEntry::Assert, "%s: (%s @ %ld) %s", typeName[assertType], filename, lineNumber, message);
154
155   return true;
156}
157
158//--------------------------------------
159const char* avar(const char *message, ...)
160{
161   static char buffer[4096];
162   va_list args;
163   va_start(args, message);
164   dVsprintf(buffer, sizeof(buffer), message, args);
165   va_end(args);
166   return( buffer );
167}
168
169//-----------------------------------------------------------------------------
170
171DefineEngineFunction(Assert, void, (bool condition, const char* message),, "Fatal Script Assertion")
172{
173   // Process Assertion.
174   AssertISV(condition, message);
175}
176