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

106
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 (5042L/28K).

please include function tempInterceptPrintIfNotIntercepted. // cmodules need this

ifdef NotifyingPrintLog
static volatile new NotifyingStringBuffer local_log; // not redirected
svoid _onLoad_print() {
  print_autoRotate();
}
endifdef
ifndef NotifyingPrintLog
static volatile new StringBuffer local_log; // not redirected
endifndef

ifdef print_rst
static new StringBuffer rst_printBuf;
static ReliableSingleThread rst_print = rst(r rst_print_flush);

svoid rst_print_flush() {
  System.out.print(getAndClearStringBuffer(rst_printBuf));
}
endifdef

sbool printAlsoToSystemOut = true;

static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot

// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;

static boolean print_silent; // total mute if set

static new O print_byThread_lock;
static volatile ThreadLocal<O> print_byThread; // special handling by thread - prefers F1<S, Bool>
static volatile O print_allThreads;
static volatile O print_preprocess;

static void print() {
  print("");
}

static <A> A mapMethodLike print(S s, A o) {
  print(combinePrintParameters(s, o));
  ret o;
}

// slightly overblown signature to return original object...
static <A> A print(A o) {
  ping_okInCleanUp();
  if (print_silent) return o;
  S s = o + "\n";
  print_noNewLine(s);
  return o;
}

static void print_noNewLine(String s) {
  ifndef LeanMode
  try {
    O f = getThreadLocal(print_byThread_dontCreate());
    if (f == null) f = print_allThreads;
      if (f != null)
        // We do need the general callF machinery here as print_byThread is sometimes shared between modules
        if (isFalse(
          ifclass F1
            f instanceof F1 ? f/F1.get(s) :
          endif
          callF(f, s))) ret;
  } catch e {
    System.out.println(getStackTrace(e));
  }
  endifndef

  print_raw(s);
}

static void print_raw(String s) {
  ifndef LeanMode
  if (print_preprocess != null) s = (S) callF(print_preprocess, s);
  s = fixNewLines(s);
  endifndef
  Appendable loc = local_log;
  Appendable buf = print_log;
  int loc_max = print_log_max;
  if (buf != loc && buf != null) {
    print_append(buf, s, print_log_max);
    loc_max = local_log_max;
  }
  if (loc != null) 
    print_append(loc, s, loc_max);
  ifdef print_rst
    rst_printBuf.append(s);
    rst_print.trigger();
  endifdef
  ifndef print_rst
  if (printAlsoToSystemOut)
    System.out.print(s);
  endifndef
  ifndef NoVMBus
  vmBus_send printed(mc(), s);
  endifndef
}

svoid print_autoRotate() {
  ifdef NotifyingPrintLog
  makeNotifyingStringBufferRotate(local_log, () -> local_log_max);
  endifdef
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000658
Snippet name: print function (=System.out.println + logging in memory)
Eternal ID of this version: #1000658/41
Text MD5: 875fa57776cb0f989485c314e8f6e799
Transpilation MD5: 93c0b78ac655bc2d4163483a2be4bc61
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-12 22:45:19
Source code size: 2782 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 1649 / 12489
Version history: 40 change(s)
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1014770 - dontPrint function - stand-in for print()
#1016424 - print_append (helper for print function)
#1034338 - print_tabToSingleSpace
#1035560 - printN2 function - print + n2
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)