% get_syslog_CRITICAL.m
% Last modified Sep. 15, 2014
% Ben Raanan

% This script retreives and logs CRITICAL messeges from syslog files

% clear 

% workd = '~/Documents/MATLAB/MBARI/mat/syslog/';
% yr=2010:2014;
% vh={'Tethys','Daphne','Makai'};

global yr vh workd 

for q=1:length(yr)
    
    year = num2str(yr(q));
    
    h = waitbar(0,'Retreving syslogs...');
    waitbar(0,h,['Retreving ' year ' syslogs...']);

    load([workd 'syslog_' year '.mat'])
    % clearvars -except workd yr year vh q v h filePath mission logName
    
    for v=1:length(vh)
        
        waitbar(0.01,h,['Scaning ' year ' syslogs for ' (vh{v}) '...']);
        formatSpec = '%s';
        for j=1:length(filePath.(vh{v}))
            
            ltmp = ['D' logName.(vh{v}){j,1}];
            mtmp = ['M' logName.(vh{v}){j,2}];
            ftmp = filePath.(vh{v}){j,1};
                      
            syslog.(vh{v}).(mtmp).(ltmp).path = ftmp;
            
            % readin syslog to struct
            fileID = fopen(ftmp);
            
            if fileID~=-1
                txt = textscan(fileID,formatSpec,'Delimiter', '\n');
                txt = txt{1,1};
                if ~isempty(txt)
                    
                    
                    % index CRITICAL messages and extract them to struct
                    ci = (not(cellfun('isempty', strfind(txt,'CRITICAL'))));
                    syslog.(vh{v}).(mtmp).(ltmp).CRITICAL = txt(ci,1);
                    
                    % get first and last lines in syslogs for runtime calc
                    rtInd = find(not(cellfun(@isempty,txt)));
                        
                        % verify time stamp is complete
                        rtin=1; rtend=length(rtInd);
                        while cellfun('isempty', strfind(txt(rtInd(rtin)),'Z'))
                            rtin =rtin+1;
                        end
                    
                        while cellfun('isempty', strfind(txt(rtInd(rtend)),'Z'))
                            rtend =rtend-1;
                        end
                    
                    syslog.(vh{v}).(mtmp).(ltmp).runTime = txt([rtInd(rtin),rtInd(rtend)],1);
                    
                    
                    fclose(fileID);
                end
            else
                warning([ftmp ': Invalid file identifier'])
                syslog.(vh{v}).(mtmp)=rmfield(syslog.(vh{v}).(mtmp),(ltmp));
            end
            clear txt ci fileID
            
            %     copyfile(['/Volumes' fileName{j,1}],[outf mission{j,1} '_' fields{j,1} '.syslog']);
            waitbar(j./length(filePath.(vh{v})),h,['Scaning ' year ' syslogs for ' vh{v} ' ['...
                num2str(100*(j./length(filePath.(vh{v}))),2) '%]'] );
            
        end; clear j tmp;
    end
    
    % save
    %
    save(['~/Documents/MATLAB/MBARI/mat/syslog/syslog_' year '.mat'],...
        'filePath','mission','logName','syslog')
    %}
    close(h)
    display([datestr(clock) ': retreived CRITICAL messeges from ' year ' syslog records'])  
end