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

97
LINES

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

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

Libraryless. Click here for Pure Java version (2485L/16K).

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  
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
23  
24  
// in bytes - will cut to half that
25  
static volatile int print_log_max = 1024*1024;
26  
static volatile int local_log_max = 100*1024;
27  
28  
static boolean print_silent; // total mute if set
29  
30  
static new O print_byThread_lock;
31  
static volatile ThreadLocal<O> print_byThread; // special handling by thread - prefers F1<S, Bool>
32  
static volatile O print_allThreads;
33  
static volatile O print_preprocess;
34  
35  
static void print() {
36  
  print("");
37  
}
38  
39  
static <A> A print(S s, A o) {
40  
  print((endsWithLetterOrDigit(s) ? s + ": " : s) + o);
41  
  ret o;
42  
}
43  
44  
// slightly overblown signature to return original object...
45  
static <A> A print(A o) {
46  
  ping_okInCleanUp();
47  
  if (print_silent) return o;
48  
  S s = o + "\n";
49  
  print_noNewLine(s);
50  
  return o;
51  
}
52  
53  
static void print_noNewLine(String s) {
54  
  ifndef LeanMode
55  
  O f = getThreadLocal(print_byThread_dontCreate());
56  
  if (f == null) f = print_allThreads;
57  
  if (f != null)
58  
    // We do need the general callF machinery here as print_byThread is sometimes shared between modules
59  
    if (isFalse(
60  
      ifclass F1
61  
        f instanceof F1 ? f/F1.get(s) :
62  
      endif
63  
      callF(f, s))) ret;
64  
  endifndef
65  
66  
  print_raw(s);
67  
}
68  
69  
static void print_raw(String s) {
70  
  ifndef LeanMode
71  
  if (print_preprocess != null) s = (S) callF(print_preprocess, s);
72  
  s = fixNewLines(s);
73  
  endifndef
74  
  Appendable loc = local_log;
75  
  Appendable buf = print_log;
76  
  int loc_max = print_log_max;
77  
  if (buf != loc && buf != null) {
78  
    print_append(buf, s, print_log_max);
79  
    loc_max = local_log_max;
80  
  }
81  
  if (loc != null) 
82  
    print_append(loc, s, loc_max);
83  
  ifdef print_rst
84  
    rst_printBuf.append(s);
85  
    rst_print.trigger();
86  
  endifdef
87  
  ifndef print_rst
88  
    System.out.print(s);
89  
  endifndef
90  
  vmBus_send printed(mc(), s);
91  
}
92  
93  
svoid print_autoRotate() {
94  
  ifdef NotifyingPrintLog
95  
  makeNotifyingStringBufferRotate(local_log, () -> local_log_max);
96  
  endifdef
97  
}

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1000658
Snippet name: print function (=System.out.println + logging in memory)
Eternal ID of this version: #1000658/36
Text MD5: 563a5afdd75040abef65b3aca5bf505a
Transpilation MD5: 5247a574bdb651bdc615bac2dd54fb1b
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-18 18:52:52
Source code size: 2583 bytes / 97 lines
Pitched / IR pitched: No / No
Views / Downloads: 971 / 11478
Version history: 35 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000658 & http://1000658.tinybrain.de