Advertisement
riccefarias76

Untitled

Apr 17th, 2024
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var spawn = require('child_process').spawn;
  2. var filename = '/opt/traccar/logs/tracker-server.log';
  3.  
  4. const fs = require("fs");
  5.  
  6. console.log("Starting logger service...");
  7.  
  8. var tail = spawn("tail", ['-f',filename]);
  9.  
  10. const imeiBindings = {};
  11. const hashBindings = {};
  12.  
  13. const tmpLogs = {};
  14.  
  15. tail.stdout.on("data", async function (data) {
  16.  
  17.     const text = data.toString();
  18.     const matches = text.matchAll(/(?<data>[0-9]{4}-[0-9]{2}-[0-9]{2}) (?<hora>[0-9]{2}:[0-9]{2}:[0-9]{2})  [A-Za-z]*: \[(?<hash>[A-Za-z0-9]*): [0-9]*( (>|<) [0-9.:]*\] \[[A-Z]*\] HEX: ([A-Za-z0-9]*)|\] [0-9.:]* .*id: (?<imeib>[A-Za-z0-9]*), (.*)|\] .*id: (?<imeic>[A-Za-z0-9]*), (.*)|\] [0-9.:]* .*)\n/gm);
  19.  
  20.  
  21.     for(m of matches){
  22.  
  23.  
  24.         let imei = false;
  25.         let hash = false;
  26.  
  27.         if(m[7]){
  28.             imei = m[7];
  29.         }else if(m[8]){
  30.             imei = m[8];
  31.         }
  32.  
  33.         if(m[3]){
  34.             hash = m[3];
  35.         }
  36.  
  37.  
  38.        
  39.         if(imei){                  
  40.             imeiBindings[imei] = hash;
  41.             hashBindings[hash] = imei;
  42.         }
  43.  
  44.         if(hashBindings[m[3]]){
  45.  
  46.             const folderName = "./logs/"+m[1];
  47.             const fileName = hashBindings[hash]+".log";
  48.            
  49.              if (!fs.existsSync(folderName)) {
  50.                     fs.mkdirSync(folderName);
  51.              }     
  52.  
  53.  
  54.             if(tmpLogs[hash]){
  55.                 for(let t of tmpLogs[hash]){
  56.                     const _folderName = "./logs/"+t[1];
  57.                     console.log("insert "+fileName+" from tmp");
  58.                     await fs.promises.appendFile(_folderName+"/"+fileName,t[0]);
  59.                 }
  60.  
  61.                 delete tmpLogs[hash];
  62.             }
  63.  
  64.  
  65.  
  66.             await fs.promises.appendFile(folderName+"/"+fileName,m[0]);
  67.  
  68.         }else{
  69.             if(!tmpLogs[hash]){
  70.                 tmpLogs[hash] = [];
  71.             }
  72.             tmpLogs[hash].push(m);
  73.         }  
  74.     }
  75.  
  76.  
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement