<%- %% DESCRIPTION This TEMPLATE needs VARIABLES: fcnToLog, inputsToLog, outputToLog, simIterLimit %> <%- %% DESCRIPTION END %> % loggingMode - mode of operation : either read or log %#codegen %#internal <%- suffix='_val'; %> <%- fcnSuffix='_TB_logger'; %> <%- origInputsToLogNames = inputsToLog; origOutputsToLogNames = outputsToLog; %> <%- nameUniqifier = coder.internal.lib.DistinctNameService( { 'loggedData', 'loggingMode', 'iterCount', 'initFlag' } ); %> <%- inputsToLog = cellfun( @(n) nameUniqifier.distinguishName(n), inputsToLog, 'UniformOutput', false); %> <%- outputsToLog = cellfun( @(n) nameUniqifier.distinguishName(n), outputsToLog, 'UniformOutput', false); %> <%- fieldsToLog = [ inputsToLog outputsToLog{:} ]; %> function loggedData = <% fcnToLog %>_logger(varargin) coder.inline('never'); coder.extrinsic('MException','throw'); persistent iterCount; if isempty(iterCount) iterCount = 0; end if nargin > 0 % Log the data. <%- for ii = 1:length(fieldsToLog) %> <%- field = fieldsToLog{ii}; %> <% [field fcnSuffix] %>(varargin{ <% ii %> }); <%- end %> iterCount = iterCount + 1; loggedData = []; if iterCount >= <% simIterLimit %> throw( MException('Coder:FXPCONV:MATLABSimBailOut', 'Return early for input computation')); end return; else % Fetch the data. % make sure the 'log setup' has been performed assert(~isempty(iterCount)); <%- for ii = 1:length(origInputsToLogNames) %> <%- field = origInputsToLogNames{ii}; %> loggedData.inputs.<% field %> = <% [inputsToLog{ii} fcnSuffix]%>(); <%- end %> <%- for ii = 1:length(origOutputsToLogNames) %> <%- field = origOutputsToLogNames{ii}; %> loggedData.outputs.<% field %> = <% [outputsToLog{ii} fcnSuffix]%>(); <%- end %> loggedData.iterCount = iterCount; end end <%- for ii = 1:length(fieldsToLog) %> <%- field = fieldsToLog{ii}; %> function out = <% [field fcnSuffix] %>(v) <%- % Prevent inlining to avoid C compiler bugs. %> coder.inline('never'); persistent p; coder.varsize('p'); if nargin == 1 if isempty(p) p = loggableValue(v); elseif size(v, 1) > 1 p = [p; loggableValue(v)]; else p = [p, loggableValue(v)]; end else assert(~isempty(p)); out = p; p(:) = []; end end <%- end %> <%- % For enums log the double value instead. %> function out = loggableValue(in) coder.inline('always'); if isenum(in) out = double(in); else out = in; end end