1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.jcmd;
  27 
  28 
  29 
  30 import java.io.IOException;
  31 import java.nio.file.Files;
  32 import java.nio.file.Paths;
  33 import java.util.List;
  34 
  35 import jdk.test.lib.dcmd.JcmdExecutor;
  36 import jdk.test.lib.dcmd.PidJcmdExecutor;
  37 
  38 /*
  39  * @test TestJcmdLogLevelChange
  40  * @key jfr
  41  * @summary Test changing log level
  42  *
  43  * @library /test/lib /test/jdk
  44  *
  45  * @run main/othervm -Xlog:jfr=info jdk.jfr.jcmd.TestJcmdChangeLogLevel
  46  */
  47 public class TestJcmdChangeLogLevel {
  48     public static void main(String[] args) throws Exception {
  49         final String fileName = "jfr_trace.txt";
  50         final String findWhat = "[info][jfr] Flight Recorder initialized";
  51         boolean passed = false;
  52 
  53         JcmdExecutor je = new PidJcmdExecutor();
  54         je.execute("VM.log output='file=" + fileName + "' what='jfr=info'");
  55         je.execute("JFR.start duration=1s");
  56         List<String> lines;
  57 
  58         do {
  59             try {
  60                 lines = Files.readAllLines(Paths.get(fileName));
  61             } catch (IOException e) {
  62                 throw new Error(e);
  63             }
  64             for (String l : lines) {
  65                 if (l.toString().contains(findWhat)) {
  66                     passed = true;
  67                     break;
  68                 }
  69             }
  70             if (lines.size() > 100) {
  71                 break; /* did not find it */
  72             }
  73         } while(!passed);
  74 
  75         if (!passed) {
  76             throw new Error("Not found " + findWhat  + " in stream" + lines);
  77         }
  78 
  79         System.out.println("PASSED");
  80     }
  81 }