blob: 22c6d83e0c9843797198c30aa487a9016170e13f [file] [log] [blame]
Taka Cho18510892019-05-14 17:37:24 -04001/*
2============LICENSE_START==========================================
3===================================================================
4Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5===================================================================
6
7Unless otherwise specified, all software contained herein is licensed
8under the Apache License, Version 2.0 (the License);
9you may not use this software except in compliance with the License.
10You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14Unless required by applicable law or agreed to in writing, software
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19
20============LICENSE_END============================================ */
21//.. srvlogger.js -logging in file system
22const fs = require('fs');
23
24const clName= "srvlogger";
25
26var MaxLogSize= 5000000;
27var LogFileNm;
28var fdL;
29
30exports.addLog = function( logStr, fileNm ) {
31
32 this.LogFileNm= fileNm
33 this.fdL= fs.openSync( this.LogFileNm, 'a' );
34 // console.log(clName+": log opened. fdL="+this.fdL);
35
36 var lfStats= fs.fstatSync( this.fdL );
37 if( lfStats.size + logStr.length >= MaxLogSize )
38 {
39 this.changeLogFile();
40 };
41
42 try {
43 fs.appendFileSync( this.fdL, logStr, 'utf8' );
44 }
45 catch( err ) {
46 console.log(clName+": log append: error:"+err.message );
47 throw err;
48 };
49 fs.closeSync( this.fdL );
50}
51
52exports.changeLogFile = function() {
53
54 var msgO= "\n=== The Log reached max size. Changing the file. ===\n";
55 try {
56 fs.appendFileSync( this.fdL, msgO, 'utf8' );
57 }
58 catch( err ) {
59 console.log(clName+": log append: error:"+err.message );
60 throw err;
61 };
62 fs.closeSync( this.fdL );
63
64 var LogFileNm_o= this.LogFileNm +".old";
65 try {
66 fs.renameSync( this.LogFileNm, LogFileNm_o );
67 }
68 catch( err ) {
69 throw err;
70 };
71
72 try {
73 this.fdL= fs.openSync( this.LogFileNm, 'a' );
74 }
75 catch( err ) {
76 console.log(clName+": New Log file open: error:["+err.message+"]\n");
77 throw err;
78 }
79 console.log( clName+": New Log file opened: fdL="+this.fdL+"\n");
80
81 var msgN= "\n=== New Log file ===\n";
82 try {
83 fs.appendFileSync( this.fdL, msgN, 'utf8' );
84 }
85 catch( err ) {
86 console.log( clName+": new log append: error:"+err.message );
87 throw err;
88 };
89}
90
91exports.setMaxLogSize = function( newMaxLogSize ) {
92 if( newMaxLogSize > 0 )
93 MaxLogSize= newMaxLogSize;
94 else
95 console.log(clName+": Wrong arg: newMaxLogSize <= 0 !");
96}