Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/dictionary/command/log.lcdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Name: log

Type: command

Syntax: log [ <argumentList> ]

Summary:
Invokes the <logMessage> if it is not empty.

Introduced: 9.5

OS: mac, windows, linux, ios, android, html5

Platforms: desktop, server, mobile

Example:
on preOpenStack
-- uBuildMode property set before building standalone
if the uBuildMode of this stack is "release" then
set the logMessage to empty
end if

loadResources
end preOpenStack

command loadResources
log "loading resources"
end loadResources

on log pInfo
-- unhandled put will go to system logs
put pInfo
end log

Parameters:
argumentList:
A comma separated list of expressions containing the arguments to send.
Arrays are expressions and are valid to send as arguments.

Description:
The <log> command invokes the <logMessage> handler. When the <logMessage> is the
default value of `log` then the <log> command behaves in the same way as any
other scripted handler. If the <logMessage> is set to empty then the <log>
command does not invoke any handler or evaluate parameters, therefore, allowing
for many logs to be added to scripts for development and an easy low-cost method
to turn the logging off for a release build. The <logMessage> may be set to any
handler name, however, if the handler is not in the message path then use of the
<log> command will throw a `can't find handler` error.

References: log (command), put (command), msgChanged (message)

Tags: debugging
47 changes: 47 additions & 0 deletions docs/dictionary/property/logmessage.lcdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Name: logMessage

Type: property

Syntax: set the logMessage to <handlerName>

Summary:
The name of the handler that is called by the <log> command.

Introduced: 9.5

OS: mac, windows, linux, ios, android, html5

Platforms: desktop, server, mobile

Example:
on preOpenStack
-- uBuildMode property set before building standalone
if the uBuildMode of this stack is "release" then
set the logMessage to empty
end if

loadResources
end preOpenStack

command loadResources
log "loading resources"
end loadResources

on log pInfo
-- unhandled put will go to system logs
put pInfo
end log

Value:
The <logMessage> is the name of the handler called by the <log> command. The
default <logMessage> is `log`. If set to empty then the <log> command does not
invoke any handler or evaluate parameters, therefore, allowing for many logs to
be added to scripts for development and an easy low-cost method to turn the
logging off for a release build. The <logMessage> may be set to any handler name,
however, if the handler is not in the message path then use of the <log> command
will throw a `can't find handler` error.

References: log (command), put (command), msgChanged (message)

Tags: debugging

34 changes: 34 additions & 0 deletions docs/notes/feature-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# New `log` command and `logMessage` property

A new command (`log`) and global property (`logMessage`) have been added to allow
an easy and low-cost method to disable or redirect script logs.

The `log` command invokes the handler named by the `logMessage` as though the
`logMessage` were directly written in the script. For backwards compatability
the default value of the `logMessage` is `log` so any scripts that currently
have a `log` handler will continue to work. To allow this `log` has been special
cased as both a command name and a permitted handler name.

If the `logMessage` is set to empty then the `log` command will not invoke any
handler or evaluate any of the parameters in the argument list.

In this example the `log` command will not be called with `pInfo` as
`loading resources` when the `uBuildMode` of the stack is `release`:

on preOpenStack
-- uBuildMode property set before building standalone
if the uBuildMode of this stack is "release" then
set the logMessage to empty
end if

loadResources
end preOpenStack

command loadResources
log "loading resources"
end loadResources

on log pInfo
-- unhandled put will go to system logs
put pInfo
end log
20 changes: 20 additions & 0 deletions engine/src/cmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,26 @@ class MCDispatchCmd: public MCStatement
virtual void exec_ctxt(MCExecContext &ctxt);
};

class MCLogCmd: public MCStatement
{
MCParameter *params;
struct
{
unsigned container_count : 16;
};

public:
MCLogCmd(void)
{
params = nullptr;
container_count = 0;
}
~MCLogCmd(void);

virtual Parse_stat parse(MCScriptPoint& sp);
virtual void exec_ctxt(MCExecContext &ctxt);
};

class MCFocus : public MCStatement
{
MCChunk *object;
Expand Down
95 changes: 95 additions & 0 deletions engine/src/cmdse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,101 @@ void MCDispatchCmd::exec_ctxt(MCExecContext &ctxt)
MCKeywordsExecTeardownCommandOrFunction(params);
}

MCLogCmd::~MCLogCmd(void)
{
while(params != NULL)
{
MCParameter *t_param;
t_param = params;
params = params -> getnext();
delete t_param;
}
}

Parse_stat MCLogCmd::parse(MCScriptPoint& sp)
{
initpoint(sp);

if (getparams(sp, &params) != PS_NORMAL)
{
MCperror -> add(PE_STATEMENT_BADPARAMS, sp);
return PS_ERROR;
}

/* If there are any parameters then compute the number of containers needed
* to execute the command. */
if (params != nullptr)
{
container_count = params->count_containers();
}

return PS_NORMAL;
}

// This method follows along the same lines as MCComref::exec
void MCLogCmd::exec_ctxt(MCExecContext &ctxt)
{
// no-op if logMessage is empty
if (MCNameIsEmpty(MClogmessage))
{
return;
}

/* Attempt to allocate the number of containers needed for the call. */
MCAutoPointer<MCContainer[]> t_containers = new MCContainer[container_count];
if (!t_containers)
{
ctxt.LegacyThrow(EE_NO_MEMORY);
return;
}

/* If the argument list is successfully evaluated, then do the dispatch. */
if (MCKeywordsExecSetupCommandOrFunction(ctxt,
params,
*t_containers,
line,
pos,
false))
{
if (!ctxt.HasError())
{
ctxt.SetLineAndPos(line, pos);
MCHandler * t_handler = nullptr;
MCKeywordsExecResolveCommandOrFunction(ctxt, MClogmessage, false, t_handler);
MCKeywordsExecCommandOrFunction(ctxt, t_handler, params, MClogmessage, line, pos, false, false);
}
}

/* Clean up the evaluated argument list */
MCKeywordsExecTeardownCommandOrFunction(params);

if (MCresultmode == kMCExecResultModeReturn)
{
// Do nothing!
}
else if (MCresultmode == kMCExecResultModeReturnValue)
{
// Set 'it' to the result and clear the result
MCAutoValueRef t_value;
if (!MCresult->eval(ctxt, &t_value))
{
ctxt.Throw();
return;
}

ctxt.SetItToValue(*t_value);
ctxt.SetTheResultToEmpty();
}
else if (MCresultmode == kMCExecResultModeReturnError)
{
// Set 'it' to empty
ctxt.SetItToEmpty();
// Leave the result as is but make sure we reset the 'return mode' to default.
MCresultmode = kMCExecResultModeReturn;
}
}


Parse_stat MCMessage::parse(MCScriptPoint &sp)
{
initpoint(sp);
Expand Down
1 change: 1 addition & 0 deletions engine/src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ MCExecContext *MCexecutioncontexts[MAX_CONTEXTS];
uint2 MCnexecutioncontexts = 0;
uint2 MCdebugcontext = MAXUINT2;
Boolean MCmessagemessages = False;
MCNameRef MClogmessage;

////////////////////////////////////////////////////////////////////////////////

Expand Down
1 change: 1 addition & 0 deletions engine/src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ extern MCExecContext *MCexecutioncontexts[MAX_CONTEXTS];
extern uint2 MCnexecutioncontexts;
extern uint2 MCdebugcontext;
extern Boolean MCmessagemessages;
extern MCNameRef MClogmessage;

struct MCExecValue;

Expand Down
19 changes: 19 additions & 0 deletions engine/src/exec-debugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,25 @@ void MCDebuggingSetWatchedVariables(MCExecContext& ctxt, MCStringRef p_value)

////////////////////////////////////////////////////////////////////////////////

void MCDebuggingGetLogMessage(MCExecContext& ctxt, MCStringRef& r_value)
{
r_value = MCValueRetain(MCNameGetString(MClogmessage));
}

void MCDebuggingSetLogMessage(MCExecContext& ctxt, MCStringRef p_value)
{
MCNewAutoNameRef t_logmessage;
if (!MCNameCreate(p_value, &t_logmessage))
{
ctxt.Throw();
return;
}

MCValueAssign(MClogmessage, *t_logmessage);
}

////////////////////////////////////////////////////////////////////////////////

void MCDebuggingExecAssert(MCExecContext& ctxt, int type, bool p_eval_success, bool p_result)
{
switch(type)
Expand Down
2 changes: 2 additions & 0 deletions engine/src/exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,8 @@ void MCDebuggingGetExecutionContexts(MCExecContext& ctxt, MCStringRef& r_value);
void MCDebuggingGetWatchedVariables(MCExecContext& ctxt, MCStringRef& r_value);
void MCDebuggingSetWatchedVariables(MCExecContext& ctxt, MCStringRef p_value);
void MCDebuggingExecPutIntoMessage(MCExecContext& ctxt, MCStringRef value, int where);
void MCDebuggingGetLogMessage(MCExecContext& ctxt, MCStringRef& r_value);
void MCDebuggingSetLogMessage(MCExecContext& ctxt, MCStringRef p_value);

///////////

Expand Down
4 changes: 4 additions & 0 deletions engine/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ void X_clear_globals(void)
#endif

MCDateTimeInitialize();

MClogmessage = MCNAME("log");
}

/* ---------------------------------------------------------------- */
Expand Down Expand Up @@ -1546,6 +1548,8 @@ int X_close(void)
if (MCcmd != nullptr)
MCValueRelease(MCcmd);

MCValueRelease(MClogmessage);

return MCretcode;
}

Expand Down
3 changes: 2 additions & 1 deletion engine/src/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ Parse_stat MCHandler::parse(MCScriptPoint &sp, Boolean isprop)

const LT *te;
// MW-2010-01-08: [[Bug 7792]] Check whether the handler name is a reserved function identifier
// special case log command is a permitted handler name
if (t_type != ST_ID ||
sp.lookup(SP_COMMAND, te) != PS_NO_MATCH ||
(sp.lookup(SP_COMMAND, te) != PS_NO_MATCH && te -> which != S_LOG) ||
(sp.lookup(SP_FACTOR, te) != PS_NO_MATCH &&
te -> type == TT_FUNCTION))
{
Expand Down
2 changes: 2 additions & 0 deletions engine/src/lextable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ const LT command_table[] =
{"load", TT_STATEMENT, S_LOAD},
{"local", TT_STATEMENT, S_LOCAL},
{"lock", TT_STATEMENT, S_LOCK},
{"log", TT_STATEMENT, S_LOG},
{"mark", TT_STATEMENT, S_MARK},
{"modal", TT_STATEMENT, S_MODAL},
{"modeless", TT_STATEMENT, S_MODELESS},
Expand Down Expand Up @@ -1173,6 +1174,7 @@ const LT factor_table[] =
{"lockupdates", TT_PROPERTY, P_LOCK_UPDATES},
{"log10", TT_FUNCTION, F_LOG10},
{"log2", TT_FUNCTION, F_LOG2},
{"logmessage", TT_PROPERTY, P_LOG_MESSAGE},
{"long", TT_PROPERTY, P_LONG},
{"longfilepath", TT_FUNCTION, F_LONG_FILE_PATH},
{"longwindowtitles", TT_PROPERTY, P_LONG_WINDOW_TITLES},
Expand Down
4 changes: 3 additions & 1 deletion engine/src/newobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ MCStatement *MCN_new_statement(int2 which)
return new MCLocalVariable;
case S_LOCK:
return new MCLock;
case S_MARK:
case S_LOG:
return new MCLogCmd;
case S_MARK:
return new MCMarkCommand;
case S_MODAL:
return new MCModal;
Expand Down
2 changes: 2 additions & 0 deletions engine/src/parsedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ enum Properties {
P_EXECUTION_CONTEXTS,
P_MESSAGE_MESSAGES,
P_WATCHED_VARIABLES,
P_LOG_MESSAGE,
P_ALLOW_INLINE_INPUT,
P_SSL_CERTIFICATES,
P_HIDE_BACKDROP,
Expand Down Expand Up @@ -2061,6 +2062,7 @@ enum Statements {
S_LOCAL,
S_LOAD,
S_LOCK,
S_LOG,
S_MARK,
S_MODAL,
S_MODELESS,
Expand Down
Loading