From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19079 invoked by alias); 6 Dec 2007 22:56:59 -0000 Received: (qmail 18997 invoked by uid 367); 6 Dec 2007 22:56:56 -0000 Date: Thu, 06 Dec 2007 22:56:00 -0000 Message-ID: <20071206225656.18982.qmail@sourceware.org> From: cagney@sourceware.org To: frysk-cvs@sourceware.org Subject: [SCM] master: Implement a really simple logger package. X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Oldrev: e15c87d557c15d6417d9065e2f6603be8d880c95 X-Git-Newrev: b752273a412939b9d582ee4cf6b8d419dc956909 Mailing-List: contact frysk-cvs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: frysk-cvs-owner@sourceware.org Reply-To: frysk@sourceware.org X-SW-Source: 2007-q4/txt/msg00536.txt.bz2 The branch, master has been updated via b752273a412939b9d582ee4cf6b8d419dc956909 (commit) from e15c87d557c15d6417d9065e2f6603be8d880c95 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email. - Log ----------------------------------------------------------------- commit b752273a412939b9d582ee4cf6b8d419dc956909 Author: Andrew Cagney Date: Thu Dec 6 17:56:44 2007 -0500 Implement a really simple logger package. ----------------------------------------------------------------------- Summary of changes: frysk-sys/frysk/rsl/ChangeLog | 13 ++ frysk-sys/frysk/rsl/Level.mkenum | 3 + frysk-sys/frysk/rsl/Log.java | 165 ++++++++++++++++++++ .../frysk/rsl/TestLog.java | 70 ++++++--- frysk-sys/frysk/rsl/package.html | 24 +++ 5 files changed, 252 insertions(+), 23 deletions(-) create mode 100644 frysk-sys/frysk/rsl/ChangeLog create mode 100644 frysk-sys/frysk/rsl/Level.mkenum create mode 100644 frysk-sys/frysk/rsl/Log.java copy frysk-core/frysk/isa/TestRegisters.java => frysk-sys/frysk/rsl/TestLog.java (60%) create mode 100644 frysk-sys/frysk/rsl/package.html First 500 lines of diff: diff --git a/frysk-sys/frysk/rsl/ChangeLog b/frysk-sys/frysk/rsl/ChangeLog new file mode 100644 index 0000000..48622a0 --- /dev/null +++ b/frysk-sys/frysk/rsl/ChangeLog @@ -0,0 +1,13 @@ +2007-12-06 Andrew Cagney + + New directory. + * Log.java: New file. + * TestLog.java: New file. + * Level.mkenum: New file. + +Local Variables: +mode: change-log +left-margin: 8 +fill-column: 74 +version-control: never +End: diff --git a/frysk-sys/frysk/rsl/Level.mkenum b/frysk-sys/frysk/rsl/Level.mkenum new file mode 100644 index 0000000..5deb651 --- /dev/null +++ b/frysk-sys/frysk/rsl/Level.mkenum @@ -0,0 +1,3 @@ +NONE 0 +FINE 1 +FINEST 2 diff --git a/frysk-sys/frysk/rsl/Log.java b/frysk-sys/frysk/rsl/Log.java new file mode 100644 index 0000000..9bbfcdf --- /dev/null +++ b/frysk-sys/frysk/rsl/Log.java @@ -0,0 +1,165 @@ +// This file is part of the program FRYSK. +// +// Copyright 2007, Red Hat Inc. +// +// FRYSK is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// FRYSK is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with FRYSK; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. +// +// In addition, as a special exception, Red Hat, Inc. gives You the +// additional right to link the code of FRYSK with code not covered +// under the GNU General Public License ("Non-GPL Code") and to +// distribute linked combinations including the two, subject to the +// limitations in this paragraph. Non-GPL Code permitted under this +// exception must only link to the code of FRYSK through those well +// defined interfaces identified in the file named EXCEPTION found in +// the source code files (the "Approved Interfaces"). The files of +// Non-GPL Code may instantiate templates or use macros or inline +// functions from the Approved Interfaces without causing the +// resulting work to be covered by the GNU General Public +// License. Only Red Hat, Inc. may make changes or additions to the +// list of Approved Interfaces. You must obey the GNU General Public +// License in all respects for all of the FRYSK code and other code +// used in conjunction with FRYSK except the Non-GPL Code covered by +// this exception. If you modify this file, you may extend this +// exception to your version of the file, but you are not obligated to +// do so. If you do not wish to provide this exception without +// modification, you must delete this exception statement from your +// version and license this file solely under the GPL without +// exception. + +package frysk.rsl; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Iterator; + +/** + * Generate log information when enabled. + */ +public final class Log { + + private int level; + private final String path; + private Log(String path, Log parent) { + this.path = path; + this.level = parent.level; + } + /** + * Create a root logger; package private so that test code can + * create their own root logger. + */ + Log() { + this.level = 0; + this.path = ""; + } + + public String toString() { + return ("{" + super.toString() + + ",path=" + path + + ",level=" + level + + "}"); + } + + private final HashMap children = new HashMap(); + public synchronized final Log set(Level level) { + this.level = level.intValue(); + for (Iterator i = children.values().iterator(); i.hasNext(); ) { + Log child = (Log)i.next(); + child.set(level); + } + return this; + } + public Level level() { + return Level.valueOf(level); + } + + /** + * POS starts at -1, then points at "." or the end of the name. + */ + synchronized final Log get(String path, int pos) { + if (pos >= path.length()) + // Reached end if the string. + return this; + // Split + int dot = path.indexOf(".", pos + 1); + if (dot < 0) + dot = path.length(); + String name = path.substring(pos + 1, dot); + Log child = (Log)children.get(name); + if (child == null) { + child = new Log(path.substring(0, dot), this); + children.put(name, child); + } + return child.get(path, dot); + } + + private static final Log root = new Log(); + public static Log get(String klass) { + return root.get(klass, -1); + } + public static Log get(Class klass) { + return root.get(klass.getName(), -1); + } + + // Static? + private static PrintStream out = System.out; + static void set(PrintStream out) { + Log.out = out; + } + + private void prefix() { + out.print(path); + out.print(": "); + } + + private void prefix(Object o) { + out.print(path); + out.print("["); + out.print(o.toString()); + out.print("]: "); + } + + private void postfix() { + out.println(); + } + + // Add at will and on demand. + private void log(String s1) { + prefix(); + out.print(s1); + postfix(); + } + public final void fine(String s1) { + log(s1); + } + public final void finest(String s1) { + log(s1); + } + + // Add at will and on demand. + private void log(Object self, String s1) { + prefix(self); + out.print(s1); + postfix(); + } + public final void fine(Object self, String s1) { + if (level < Level.FINE_) + return; + log(self, s1); + } + public final void finest(Object self, String s1) { + if (level < Level.FINEST_) + return; + log(self, s1); + } +} diff --git a/frysk-core/frysk/isa/TestRegisters.java b/frysk-sys/frysk/rsl/TestLog.java similarity index 60% copy from frysk-core/frysk/isa/TestRegisters.java copy to frysk-sys/frysk/rsl/TestLog.java index 7fc0d44..88f4eef 100644 --- a/frysk-core/frysk/isa/TestRegisters.java +++ b/frysk-sys/frysk/rsl/TestLog.java @@ -1,11 +1,11 @@ // This file is part of the program FRYSK. -// +// // Copyright 2007, Red Hat Inc. -// +// // FRYSK is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. -// +// // FRYSK is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -37,42 +37,66 @@ // version and license this file solely under the GPL without // exception. -package frysk.isa; +package frysk.rsl; import frysk.junit.TestCase; /** - * The set of registers belonging to an ISA. + * Testlogging that is a sub-class of this directory. */ -public class TestRegisters extends TestCase { - private Registers regs; +public class TestLog extends TestCase { + //private static final Log log = Log.get(TestCase.class).set(Level.FINE); + + private Log root; public void setUp() { - regs = new IA32Registers(); + root = new Log(); } public void tearDown() { - regs = null; + root = null; + } + private Log get(String path) { + return root.get(path, -1); } - public void testGetGroup() { - assertEquals("getGroup", IA32Registers.REGS_GROUP, - regs.getGroup("regs")); + public void testRoot() { + //log.fine(this, "hello"); + assertNotNull("root", root); } - public void testGetRegister() { - assertEquals("getGroup", IA32Registers.FS, - regs.getRegister("fs")); + public void testGetSelf() { + Log self = get("self"); + assertNotNull("self", self); } - public void testGetGroupNames() { - assertEquals("getGroupNames", - new String[] { - "regs", "float", "vector", "segment" - }, regs.getGroupNames()); + public void testPeers() { + Log lhs = get("the.lhs"); + Log rhs = get("the.rhs"); + assertNotNull("the.lhs", lhs); + assertNotNull("the.rhs", rhs); + assertTrue("lhs != rhs", lhs != rhs); + assertEquals("the.lhs", lhs, get("the.lhs")); + assertEquals("the.rhs", rhs, get("the.rhs")); } - public void testGeneralRegisterGroup() { - assertEquals("getGeneralRegisterGroup", IA32Registers.REGS_GROUP, - regs.getGeneralRegisterGroup()); + private void checkLevel(String path, Level level) { + assertEquals("level " + path, level, get(path).level()); + } + private void set(String path, Level level) { + get(path).set(level); } + public void testLeveling() { + // create a tree + get("the.lower.left.hand.side"); + get("the.lower.right.hand.side"); + // set a level + set("the.lower.left", Level.FINE); + checkLevel("the", Level.NONE); + checkLevel("the.lower", Level.NONE); + checkLevel("the.lower.left", Level.FINE); + checkLevel("the.lower.left.hand", Level.FINE); + checkLevel("the.lower.left.hand.side", Level.FINE); + checkLevel("the.lower.right", Level.NONE); + } + } diff --git a/frysk-sys/frysk/rsl/package.html b/frysk-sys/frysk/rsl/package.html new file mode 100644 index 0000000..d93c3d3 --- /dev/null +++ b/frysk-sys/frysk/rsl/package.html @@ -0,0 +1,24 @@ + + + +This package provides a really simple logger mechanism. + +

Overview

+ +The {@link frysk.rsl} package provides the client with a really simple +logging mechanism. + +

+ +Create a logger using: +

+private final Log log = Log.get(MyClass.class);
+
+and use it with: +
+log.fine("a message"); // static
+log.finest(this, "a message"); // dynamic
+
+ + + hooks/post-receive -- frysk system monitor/debugger