function logLevel = getWorkerLogLevel()
% getWorkerLogLevel - get the current worker log level from the environment
%
% Returns 0 if the debug flag is not set. Otherwise it attempts to
% parse the debug setting string retrieved from the environment and
% if it succeeds it returns that value, otherwise it returns the
% maximum log level 6.

% Copyright 2018 The MathWorks, Inc.

debugSetting = parallel.internal.logging.getWorkerDebugSetting();
% Debugging is enabled if debugSetting is set to anything other
% than 'false'
isEnabled = ~isempty(debugSetting) && ~strcmpi(debugSetting, 'false');
if isEnabled
    debugLevel = str2double(debugSetting);
    if ~isnan(debugLevel) && any(debugLevel == [0 1 2 3 4 5 6])
        logLevel = debugLevel;
    else
        LOG_LEVEL_ALL = 6;
        logLevel = LOG_LEVEL_ALL;
    end
else
    % Log only level 0 messages if debugFlag isn't defined or is 
    % 'false'
    LOG_LEVEL_SEVERE = 0;
    logLevel = LOG_LEVEL_SEVERE;
end
