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

81
LINES

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

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

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

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, 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/33
Text MD5: c8981f2dd487f91801ee6cf716215455
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-23 22:04:56
Source code size: 2226 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 836 / 11224
Version history: 32 change(s)
Referenced in: [show references]

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