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

48
LINES

< > BotCompany Repo | #1015195 // TailFile

JavaX fragment (include)

1  
sclass TailFile {
2  
  File file;
3  
  int interval;
4  
  long l;
5  
  new Flag stopped;
6  
  java.util.Timer timer;
7  
  ReliableSingleThread thread;
8  
  volatile bool hasIdled, debug;
9  
  
10  
  *() {}
11  
  *(final File *file, int *interval, fO onData) {
12  
    thread = new ReliableSingleThread(r {
13  
      long l2 = l(file);
14  
      if (l2 < l) l = 0; // file shrunk (log rotated!) - start over from beginning of file
15  
      if (l2 == l) hasIdled = true;
16  
      else pcall {
17  
        if (debug) print("New data in " + f2s(file) + ": " + n2(l2-l, "byte"));
18  
        S text = null;
19  
        for (S s : loadTextFilePart_iterator(file, l, l2))
20  
          pcallF(onData, s);
21  
        l = l2;
22  
      }
23  
    });
24  
  }
25  
  
26  
  void start {
27  
    timer = doEvery(interval, r {
28  
      if (!stopped.isUp()) thread.trigger()
29  
    });
30  
  }
31  
  
32  
  void stop {
33  
    timer.cancel();
34  
    timer = null;
35  
    if (stopped.raise())
36  
      thread.triggerAndWait(); // One last time baby
37  
  }
38  
  
39  
  bool started() {
40  
    ret timer != null;
41  
  }
42  
  
43  
  void debugOn {
44  
    if (debug) ret;
45  
    debug = true;
46  
    print("Watching file: " + f2s(file));
47  
  }
48  
}

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: 349 / 381
Referenced in: [show references]