Skip to content

Commit 3abbb94

Browse files
committed
Added option 'log_script_errors' for logging script errors
1 parent f0890e2 commit 3abbb94

File tree

6 files changed

+106
-18
lines changed

6 files changed

+106
-18
lines changed

doc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ class CMUSHclientDoc : public CDocument
899899
unsigned int m_iToolTipVisibleTime; // Time tooltip stays visible (milliseconds)
900900
unsigned int m_iToolTipStartTime; // Time before tooltip appears (milliseconds)
901901

902+
903+
// version 4.92
904+
unsigned short m_bLogScriptErrors; // write scripting error messages to log file?
905+
902906
// end of stuff saved to disk **************************************************************
903907

904908
// stuff from pre version 11, read from disk but not saved
@@ -1914,6 +1918,7 @@ class CMUSHclientDoc : public CDocument
19141918
);
19151919

19161920
void ShowErrorLines (const int iLine); // show script file around the error point
1921+
void WriteErrorLines (const int iLine, FILE * f); // write script file around the error point to file f
19171922

19181923
void ShowStatusLine (const bool bNow = false); // update the status line
19191924

doc_construct.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ int i;
112112
m_bDoNotShowOutstandingLines = false;
113113
m_bDoNotTranslateIACtoIACIAC = false;
114114
m_bAutoResizeCommandWindow = false;
115+
m_bLogScriptErrors = false;
115116
m_iAutoResizeMinimumLines = 1;
116117
m_iAutoResizeMaximumLines = 20;
117118
m_bDoNotAddMacrosToCommandHistory = false;

plugins/Config_Option_Changer.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ boolean_options = {
6666
do_not_add_macros_to_command_history = { desc = 'Add macros to command history?' , invert = true },
6767
do_not_show_outstanding_lines = { desc = 'Show outstanding lines count?' , invert = true },
6868
do_not_translate_iac_to_iac_iac = { desc = 'Translate IAC to IAC IAC?' , invert = true },
69+
log_script_errors = { desc = 'Log scripting errors?' },
6970
play_sounds_in_background = { desc = 'Play sounds in background?' },
7071
send_keep_alives = { desc = 'Send keep-alives?' },
7172
wrap_input = { desc = 'Wrap command window at output wrap column' },

scripting/lua_scripting.cpp

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -372,45 +372,85 @@ void LuaError (lua_State *L,
372372
dlg.m_strRaisedBy = "World: " + pDoc->m_mush_name;
373373
}
374374

375+
// work out the line number where the error is
376+
bool bImmediate = true;
377+
int nLine = 0;
378+
379+
if (dlg.m_strDescription.Left (18) == "[string \"Plugin\"]:")
380+
{
381+
bImmediate = false;
382+
nLine = atoi (dlg.m_strDescription.Mid (18));
383+
}
384+
else if (dlg.m_strDescription.Left (23) == "[string \"Script file\"]:")
385+
{
386+
bImmediate = false;
387+
nLine = atoi (dlg.m_strDescription.Mid (23));
388+
}
389+
390+
// if no document, or errors to the output window are not wanted, display the dialog box
375391
if (!pDoc || !pDoc->m_bScriptErrorsToOutputWindow)
376392
{
377393
if (pDoc)
378394
dlg.m_bHaveDoc = true;
379395
dlg.DoModal ();
396+
397+
// do they want to change from the dialog box to the output window?
380398
if (pDoc && dlg.m_bUseOutputWindow)
381399
{
382400
pDoc->m_bScriptErrorsToOutputWindow = true;
383401
pDoc->SetModifiedFlag (TRUE);
384-
}
402+
} // end of future errors wanted in output window
385403

386-
}
404+
} // end of dialog box wanted
387405
else
388406
{
389407
pDoc->ColourNote (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, strEvent);
390408
pDoc->ColourNote (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, dlg.m_strRaisedBy);
391409
pDoc->ColourNote (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, dlg.m_strCalledBy);
392410
pDoc->ColourNote (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, dlg.m_strDescription);
393411

394-
// show bad lines?
412+
// show bad lines?
413+
if (!bImmediate)
414+
pDoc->ShowErrorLines (nLine);
415+
416+
} // end of showing in output window
395417

396-
bool bImmediate = true;
397-
int nLine = 0;
398418

399-
if (dlg.m_strDescription.Left (18) == "[string \"Plugin\"]:")
419+
// if option "log_script_errors" is active, append to the error log file
420+
if (pDoc && pDoc->m_bLogScriptErrors)
421+
{
422+
string fileName = App.m_strDefaultLogFileDirectory;
423+
fileName += "script_error_log.txt";
424+
425+
FILE * errorLogFile = fopen (fileName.c_str(), "a+");
426+
if (!errorLogFile)
400427
{
401-
bImmediate = false;
402-
nLine = atoi (dlg.m_strDescription.Mid (18));
428+
pDoc->ColourTell (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, "Cannot open error log file: ");
429+
pDoc->ColourNote (SCRIPTERRORFORECOLOUR, SCRIPTERRORBACKCOLOUR, fileName.c_str());
403430
}
404-
else if (dlg.m_strDescription.Left (23) == "[string \"Script file\"]:")
431+
else
405432
{
406-
bImmediate = false;
407-
nLine = atoi (dlg.m_strDescription.Mid (23));
408-
}
409-
410-
if (!bImmediate)
411-
pDoc->ShowErrorLines (nLine);
412-
413-
} // end of showing in output window
433+
CTime timeNow = CTime::GetCurrentTime();
434+
435+
CString strConnected = timeNow.Format (
436+
TranslateTime ("\n\n--- Scripting error on %A, %B %d, %Y, %#I:%M %p ---\n\n"));
437+
fputs ((LPCTSTR) strConnected, errorLogFile);
438+
fputs (strEvent, errorLogFile);
439+
fputs ("\n", errorLogFile);
440+
fputs (dlg.m_strRaisedBy, errorLogFile);
441+
fputs ("\n", errorLogFile);
442+
fputs (dlg.m_strCalledBy, errorLogFile);
443+
fputs ("\n", errorLogFile);
444+
fputs (dlg.m_strDescription, errorLogFile);
445+
fputs ("\n", errorLogFile);
446+
// show bad lines?
447+
if (!bImmediate)
448+
pDoc->WriteErrorLines (nLine, errorLogFile);
449+
fclose (errorLogFile);
450+
} // end of file opened OK
451+
452+
453+
} // end of have a document, and the error file is wanted
414454

415455
} // end of LuaError
416456

scripting/scripting.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,45 @@ vector<string> v;
532532

533533
} // end of line in range
534534

535-
}
535+
} // end of CMUSHclientDoc::ShowErrorLines
536+
537+
538+
void CMUSHclientDoc::WriteErrorLines (const int iLine, FILE * f) // show script file around the error point
539+
{
540+
541+
string sScript;
542+
vector<string> v;
543+
544+
if (m_CurrentPlugin)
545+
sScript = m_CurrentPlugin->m_strScript;
546+
else
547+
sScript = m_strScript;
548+
549+
StringToVector (sScript, v, "\n", false); // break script into lines so we can easily show each one
550+
551+
// provided wanted line is in the table
552+
if (!sScript.empty () && v.size () >= iLine)
553+
{
554+
fputs (Translate ("Error context in script:\n"), f);
555+
556+
int iStart = iLine - 4; // start 4 lines back
557+
if (iStart < 1)
558+
iStart = 1; // can't be lower than first line
559+
560+
int iEnd = iLine + 4; // end 4 lines onwards
561+
562+
if (iEnd > v.size ())
563+
iEnd = v.size (); // or at last line in script
564+
565+
// show that range, marking error line with an asterisk
566+
for (int i = iStart; i <= iEnd; i++)
567+
fputs (CFormat ("%4i%s: %s\n",
568+
i, // line number
569+
i == iLine ? "*" : " ", // mark current line
570+
(v [i - 1]).c_str ()), // vector is zero-relative
571+
f);
572+
573+
} // end of line in range
574+
575+
} // end of CMUSHclientDoc::WriteErrorLines
536576

scriptingoptions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ tConfigurationNumericOption OptionsTable [] = {
135135
{"log_notes", false, O(m_bLogNotes)},
136136
{"log_output", true, O(m_bLogOutput)},
137137
{"log_raw", false, O(m_bLogRaw)},
138+
{"log_script_errors", false, O(m_bLogScriptErrors)},
138139
{"lower_case_tab_completion", false, O(m_bLowerCaseTabCompletion)},
139140
{"map_failure_regexp", false, O(m_bMapFailureRegexp)},
140141
{"max_output_lines", 5000, O(m_maxlines), 200, 500000, OPT_FIX_OUTPUT_BUFFER},

0 commit comments

Comments
 (0)