Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

48
LINES

< > BotCompany Repo | #1015195 // TailFile

JavaX fragment (include)

sclass TailFile {
  File file;
  int interval;
  long l;
  new Flag stopped;
  java.util.Timer timer;
  ReliableSingleThread thread;
  volatile bool hasIdled, debug;
  
  *() {}
  *(final File *file, int *interval, fO onData) {
    thread = new ReliableSingleThread(r {
      long l2 = l(file);
      if (l2 < l) l = 0; // file shrunk (log rotated!) - start over from beginning of file
      if (l2 == l) hasIdled = true;
      else pcall {
        if (debug) print("New data in " + f2s(file) + ": " + n2(l2-l, "byte"));
        S text = null;
        for (S s : loadTextFilePart_iterator(file, l, l2))
          pcallF(onData, s);
        l = l2;
      }
    });
  }
  
  void start {
    timer = doEvery(interval, r {
      if (!stopped.isUp()) thread.trigger()
    });
  }
  
  void stop {
    timer.cancel();
    timer = null;
    if (stopped.raise())
      thread.triggerAndWait(); // One last time baby
  }
  
  bool started() {
    ret timer != null;
  }
  
  void debugOn {
    if (debug) ret;
    debug = true;
    print("Watching file: " + f2s(file));
  }
}

Author comment

Began life as a copy of #1013001

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015195
Snippet name: TailFile
Eternal ID of this version: #1015195/1
Text MD5: b7d14f36ef54a1bebd3e54bb3f4da8b1
Author: stefan
Category: javax / i.o.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-10 17:47:22
Source code size: 1114 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 348 / 380
Referenced in: [show references]