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

104
LINES

< > BotCompany Repo | #1000658 - print function (=System.out.println + logging in memory)

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (2965L) is out of date.

1  
please include function tempInterceptPrintIfNotIntercepted. // cmodules need this
2  
3  
ifdef NotifyingPrintLog
4  
static volatile new NotifyingStringBuffer local_log; // not redirected
5  
svoid _onLoad_print() {
6  
  print_autoRotate();
7  
}
8  
endifdef
9  
ifndef NotifyingPrintLog
10  
static volatile new StringBuffer local_log; // not redirected
11  
endifndef
12  
13  
ifdef print_rst
14  
static new StringBuffer rst_printBuf;
15  
static ReliableSingleThread rst_print = rst(r rst_print_flush);
16  
17  
svoid rst_print_flush() {
18  
  System.out.print(getAndClearStringBuffer(rst_printBuf));
19  
}
20  
endifdef
21  
22  
sbool printAlsoToSystemOut = true;
23  
24  
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
25  
26  
// in bytes - will cut to half that
27  
static volatile int print_log_max = 1024*1024;
28  
static volatile int local_log_max = 100*1024;
29  
30  
static boolean print_silent; // total mute if set
31  
32  
static new O print_byThread_lock;
33  
static volatile ThreadLocal<O> print_byThread; // special handling by thread - prefers F1<S, Bool>
34  
static volatile O print_allThreads;
35  
static volatile O print_preprocess;
36  
37  
static void print() {
38  
  print("");
39  
}
40  
41  
static <A> A mapMethodLike print(S s, A o) {
42  
  print(combinePrintParameters(s, o));
43  
  ret o;
44  
}
45  
46  
// slightly overblown signature to return original object...
47  
static <A> A print(A o) {
48  
  ping_okInCleanUp();
49  
  if (print_silent) return o;
50  
  S s = o + "\n";
51  
  print_noNewLine(s);
52  
  return o;
53  
}
54  
55  
static void print_noNewLine(String s) {
56  
  ifndef LeanMode
57  
  try {
58  
    O f = getThreadLocal(print_byThread_dontCreate());
59  
    if (f == null) f = print_allThreads;
60  
      if (f != null)
61  
        // We do need the general callF machinery here as print_byThread is sometimes shared between modules
62  
        if (isFalse(
63  
          ifclass F1
64  
            f instanceof F1 ? f/F1.get(s) :
65  
          endif
66  
          callF(f, s))) ret;
67  
  } catch e {
68  
    System.out.println(getStackTrace(e));
69  
  }
70  
  endifndef
71  
72  
  print_raw(s);
73  
}
74  
75  
static void print_raw(String s) {
76  
  ifndef LeanMode
77  
  if (print_preprocess != null) s = (S) callF(print_preprocess, s);
78  
  s = fixNewLines(s);
79  
  endifndef
80  
  Appendable loc = local_log;
81  
  Appendable buf = print_log;
82  
  int loc_max = print_log_max;
83  
  if (buf != loc && buf != null) {
84  
    print_append(buf, s, print_log_max);
85  
    loc_max = local_log_max;
86  
  }
87  
  if (loc != null) 
88  
    print_append(loc, s, loc_max);
89  
  ifdef print_rst
90  
    rst_printBuf.append(s);
91  
    rst_print.trigger();
92  
  endifdef
93  
  ifndef print_rst
94  
  if (printAlsoToSystemOut)
95  
    System.out.print(s);
96  
  endifndef
97  
  vmBus_send printed(mc(), s);
98  
}
99  
100  
svoid print_autoRotate() {
101  
  ifdef NotifyingPrintLog
102  
  makeNotifyingStringBufferRotate(local_log, () -> local_log_max);
103  
  endifdef
104  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000658
Snippet name: print function (=System.out.println + logging in memory)
Eternal ID of this version: #1000658/40
Text MD5: a1fc34a07d9e23cfa1fab34c4ddf9c53
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-02 06:04:29
Source code size: 2751 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 1163 / 11784
Version history: 39 change(s)
Referenced in: [show references]