4 function configure(varargin)
6 import org.apache.log4j.Logger;
7 className=mfilename('class');
8 feval([className '.flush']);
9 %% default values for properties
10 storageLocation=[fileparts(which(className))
filesep 'DataLogs'];
12 functionNameList=cell(1,0);
18 switch lower(prop{iProp})
20 isEnabled=prop{iProp+1};
21 case 'functionnamelist' 22 functionNameList=prop{iProp+1};
23 case 'storagelocationroot' 24 storageLocation=prop{iProp+1};
25 case 'nmaxdatesondisk' 26 nMaxDatesOnDisk=prop{iProp+1};
29 'Unknown property: %s',prop{iProp});
34 if ~(islogical(isEnabled)&&numel(isEnabled)==1)
36 'isEnabled must be scalar logical');
38 % check functionNameList
39 if ischar(functionNameList)
40 if isempty(functionNameList)||size(functionNameList,2)~=...
41 numel(functionNameList)
43 'functionNameList must be nonempty string');
45 functionNameList={functionNameList};
47 isnWrong=iscell(functionNameList);
49 functionNameList=reshape(functionNameList,1,[]);
51 cellfun(
'isclass',functionNameList,
'char')&...
52 cellfun(
'size',functionNameList,2)==...
53 cellfun(
'prodofsize',functionNameList)&...
54 ~cellfun(
'isempty',functionNameList));
58 'functionNameList must be cell array with nonempty strings');
60 functionNameList=
unique(functionNameList);
62 % check storageLocation
63 if isempty(storageLocation)||~(ischar(storageLocation)&&...
64 size(storageLocation,2)==numel(storageLocation))
66 'storageLocation must be nonempty string');
68 % check nMaxDatesOnDisk
69 isnWrong=isnumeric(nMaxDatesOnDisk)&&numel(nMaxDatesOnDisk)==1;
71 nMaxDatesOnDisk=double(nMaxDatesOnDisk);
72 isnWrong=~isnan(nMaxDatesOnDisk)&&nMaxDatesOnDisk>=0&&...
73 floor(nMaxDatesOnDisk)==nMaxDatesOnDisk;
77 'nMaxDatesOnDisk must be scalar nonnegative integer');
79 %% preprocess data storage
83 if isfinite(nMaxDatesOnDisk)&&isEnabled
84 %
delete files that are more than nMaxDatesOnDisk dates on
86 SFileList=dir(storageLocation);
87 SFileList([SFileList.isdir])=[];
88 fileNameList={SFileList.name};
89 if ~isempty(fileNameList)
90 [~,fileNameList,extNameList]=cellfun(@fileparts,...
91 fileNameList,
'UniformOutput',
false);
92 isFileVec=strcmp(extNameList,
'.mat');
94 fileNameList=fileNameList(isFileVec);
95 fileNameList=fileNameList(...
96 [SFileList(isFileVec).datenum]<=...
97 addtodate(floor(now),-nMaxDatesOnDisk,
'day'));
98 nFiles=numel(fileNameList);
100 fileNameList=strcat(storageLocation,
filesep,fileNameList,
'.mat');
102 delete(fileNameList{iFile});
109 setMethodName=[className
'.setPropInternal'];
110 feval(setMethodName,
'isEnabled',isEnabled);
111 feval(setMethodName,
'functionNameList',functionNameList);
112 feval(setMethodName,
'prefixList',cell(1,0));
113 feval(setMethodName,
'loggerObj',Logger.getLogger(className));
114 feval(setMethodName,
'storageLocation',storageLocation);
115 feval(setMethodName,
'filePostfix',
mxberry.
system.getpidhost());
118 function addPrefixToList(prefixStr)
121 className=mfilename(
'class');
122 if ~feval([className
'.getIsEnabled'])
125 if isempty(prefixStr)||~(...
126 ischar(prefixStr)&&size(prefixStr,2)==numel(prefixStr))
128 'prefixStr must be nonemtpy string');
131 prefixList=feval([className
'.getPropInternal'],
'prefixList');
132 feval([className
'.setPropInternal'],
'prefixList',horzcat(prefixList,{prefixStr}));
135 function removePrefixFromList()
138 className=mfilename(
'class');
139 if ~feval([className
'.getIsEnabled'])
143 prefixList=feval([className
'.getPropInternal'],
'prefixList');
144 if isempty(prefixList)
146 'addPrefixToList must be called before calling this method');
148 feval([className
'.setPropInternal'],
'prefixList',prefixList(1:end-1));
153 className=mfilename('class');
154 if ~feval([className '.getIsEnabled'])
157 [~,fullFuncName,isLogged]=feval([className '.getFunctionProps'],2);
161 getMethodName=[className '.getPropInternal'];
162 loggerObj=feval(getMethodName,'loggerObj');
163 loggerObj.info([fullFuncName ' is performed']);
168 className=mfilename('class');
169 if ~feval([className '.getIsEnabled'])
172 [shortFuncName,fullFuncName,isLogged]=feval([className '.getFunctionProps'],2);
176 loggerObj=feval([className '.getPropInternal'],'loggerObj');
177 fileName=feval([className '.getDataFileName'],shortFuncName,fullFuncName);
178 [~,shortFileName]=fileparts(fileName);
179 loggerObj.info([fullFuncName ' is performed, data with local variables are in ' shortFileName]);
180 evalin('caller',['save(' fileName ')']);
185 className=mfilename('class');
186 feval([className '.flushInternal'],className);
190 methods (Access=protected,Static)
191 function isEnabled=getIsEnabled()
193 className=mfilename('class');
194 [isEnabled,isConfigured]=feval([className '.getPropInternal'],...
195 'isEnabled',true,className);
196 isEnabled=isConfigured&&~isempty(isEnabled)&&isEnabled;
199 function fileName=getDataFileName(shortFuncName,fullFuncName)
201 className=mfilename('class');
202 getMethodName=[className '.getPropInternal'];
203 fileName=[feval(getMethodName,'storageLocation')
filesep ...
204 shortFuncName '_' feval(getMethodName,'filePostfix') '_'...
205 datestr(now,30) '_'
mxberry.core.
hash(fullFuncName) '.mat'];
208 function [shortFuncName,fullFuncName,isLogged]=getFunctionProps(indStack)
210 curClassName=mfilename('class');
211 %% find function names (
short and full one, but without
213 StFunc=dbstack('-completenames');
214 nFuncs=numel(StFunc);
222 for iFunc=indStack:nFuncs
223 % determine name of class (if any) and name of
224 % method (function or script)
225 StFuncCur=StFunc(iFunc);
226 [pathStr,fileName]=fileparts(StFuncCur.file);
227 pathStr=strrep(pathStr,[
filesep '+'],'.');
228 indClass=strfind(pathStr,[
filesep '@']);
229 isClass=~isempty(indClass);
231 pathStr(indClass)='.';
232 pathStr(indClass+1)=[];
234 curInd=find(pathStr==
filesep,1,'last');
236 pathStr=pathStr(curInd+1:end);
238 curInd=find(pathStr=='.',1,'first');
242 pathStr=pathStr(curInd+1:end);
243 isPath=~isempty(pathStr);
245 methodName=StFuncCur.name;
246 curInd=find(methodName=='.',1,'first');
248 methodName=methodName(curInd+1:end);
250 pathStr=[pathStr '.' fileName]; %
#ok<AGROW> 254 shortFuncName=methodName(find([
'/' methodName]==
'/',1,
'last'):end);
260 methodName=[pathStr
'.' methodName]; %#ok<AGROW>
264 isFound=isempty(className);
266 isFound=~any(strcmp(superclasses(className),curClassName));
272 fullFuncName=methodName;
273 if ~isempty(className)
274 fullFuncName=[className
'.' fullFuncName]; %#ok<AGROW>
281 'This method must be called from function, script or '...
282 'method of some class different from descendants of %s'],...
285 %% process fullFuncName and isLogged
287 getMethodName=[curClassName
'.getPropInternal'];
288 prefixList=feval(getMethodName,
'prefixList');
289 fullFuncName=
cell2tablestr([],[prefixList {fullFuncName}],
'.');
291 isLogged=any(strcmp(fullFuncName,...
292 feval(getMethodName,
'functionNameList')));
297 function [propVal,isThere]=getPropInternal(propName,isPresenceChecked,className)
300 branchName=className;
302 branchName=mfilename(
'class');
305 isPresenceChecked=
false;
308 branchName,propName,isPresenceChecked);
311 function setPropInternal(propName,propVal,className)
314 branchName=className;
316 branchName=mfilename(
'class');
319 branchName,propName,propVal);
static function setPropInternal(in branchName, in propName, in propVal)
function hash(in inpArr, in methodName)
OBJECTHASH counts the hash of input object/array.
function cell2tablestr(in titleList, in dataCell, in colSepSeq, in varargin)
CELL2TABLESTR - converts a cell array into a table-like char array (or a cell array of strings repres...
function unique(in inpVec)
UNIQUE for arrays of any type.
static function getPropInternal(in branchName, in propName, in isPresenceChecked)
function parseparams(in args, in propNameList, in nRegExpected, in nPropExpected)
PARSEPARAMS behaves exactly as a built-in Matlab function apart from the incorrect behavior of Matlab...