Taka Cho | 1851089 | 2019-05-14 17:37:24 -0400 | [diff] [blame^] | 1 | /* |
| 2 | ============LICENSE_START========================================== |
| 3 | =================================================================== |
| 4 | Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. |
| 5 | =================================================================== |
| 6 | |
| 7 | Unless otherwise specified, all software contained herein is licensed |
| 8 | under the Apache License, Version 2.0 (the License); |
| 9 | you may not use this software except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | |
| 20 | ============LICENSE_END============================================ */ |
| 21 | //.. srvlogger.js -logging in file system |
| 22 | const fs = require('fs'); |
| 23 | |
| 24 | const clName= "srvlogger"; |
| 25 | |
| 26 | var MaxLogSize= 5000000; |
| 27 | var LogFileNm; |
| 28 | var fdL; |
| 29 | |
| 30 | exports.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 | |
| 52 | exports.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 | |
| 91 | exports.setMaxLogSize = function( newMaxLogSize ) { |
| 92 | if( newMaxLogSize > 0 ) |
| 93 | MaxLogSize= newMaxLogSize; |
| 94 | else |
| 95 | console.log(clName+": Wrong arg: newMaxLogSize <= 0 !"); |
| 96 | } |