Project

General

Profile

outputToFile-in-java.diff

Galya B, 12/15/2022 01:07 PM

Download (2.22 KB)

View differences:

new/src/com/goldencode/p2j/main/ClientDriver.java 2022-12-15 18:06:37 +0000
406 406
         // normally should not occur.
407 407
      }      
408 408
   }
409
   
409

  
410
   private static void setBgrProcessLog(String[] args) {
411
      String outputToFileConfig = Arrays.stream(args)
412
                                        .filter(arg -> arg != null && arg.startsWith("clientConfig:outputToFile"))
413
                                        .findAny()
414
                                        .orElse(null);
415
      if (outputToFileConfig == null)
416
      {
417
         return;
418
      }
419

  
420
      String outputToFile = outputToFileConfig.replace("clientConfig:outputToFile=", "");
421
      try
422
      {
423
         outputToFile = outputToFile.replace("%pid%", getCurrentProcessPid().toString());
424
         System.setOut(new PrintStream(new FileOutputStream(outputToFile), true));
425
      }
426
      catch (Exception e)
427
      {
428
         System.err.println("ClientDriver: Unable to redirect stdout to clientConfig:outputToFile " + outputToFile);
429
      }
430
   }
431

  
432
   private static Integer getCurrentProcessPid() throws Exception {
433
      java.lang.management.RuntimeMXBean runtime =
434
         java.lang.management.ManagementFactory.getRuntimeMXBean();
435
      java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
436
      jvm.setAccessible(true);
437
      sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
438
      java.lang.reflect.Method pid_method =
439
         mgmt.getClass().getDeclaredMethod("getProcessId");
440
      pid_method.setAccessible(true);
441

  
442
      return (Integer) pid_method.invoke(mgmt);
443
   }
444

  
410 445
   /**
411 446
    * The command line entry point.
412 447
    * 
......
417 452
   {
418 453
      // for web and batch clients only redirect stderr to a log file
419 454
      setClientLog(args);
420
      
455

  
456
      // TODO: fix when spawner sends the arg
457
      setBgrProcessLog(new String[] { "clientConfig:outputToFile=output_to_file_%pid%.log" });
458

  
421 459
      ClientDriver client = new ClientDriver();
422 460
      
423 461
      // after our parent class processes the command line arguments, if there are no errors,