classdef BuildLogger < handle
%BuildLogger captures results from running a build stage

% Copyright 2019-2020 The MathWorks, Inc.
    
    properties(SetAccess=private, GetAccess=public, Hidden=true)
        Log = '';
    end
    
    properties(Access=private)
        Verbose = false;
        InfoFcn;
    end
    
    methods(Access=public)
        function this = BuildLogger(lVerbose, lDispHook)
            this.Verbose = lVerbose;
            if isempty(lDispHook)
                this.InfoFcn = @disp;
            else
                this.InfoFcn = lDispHook;
            end
        end
        
        function val = isVerboseBuild(this)
            val = this.Verbose;
        end

        function addToBuildLogOnly(this, text)
            % Add text to the build log (assume it has already been
            % displayed (in verbose case) via a call to system with
            % -echo set
            if isempty(text)
                return
            end
            text = char(text);
            this.Log = [this.Log text newline];
        end

        function addToBuildLog(this, text)
            % Add text to the build log and, for verbose build, display it
            % as well
            if isempty(text)
                return
            end
            text = char(text);
            this.Log = [this.Log text newline];
            if this.Verbose
                % Send output to either command window or diagnostic viewer
                this.InfoFcn(text);
            end
        end
        
        function forceDisplay(this, text)
            % Send output to either command window or diagnostic viewer
            % (even if Verbose is false)
            text = char(text);
            this.InfoFcn(text);
        end
        
        function disp(this)
            disp(this.Log);
        end
        
    end
end
