Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] around() Problem

As David's code demonstrates, the reason call(void System.out.println(..)) doesn't work is that System.out is an object (a field in System), not a class. 

Here's an additional useful idiom, you can also catch where you're using System.out or System.err to refactor it to use the log:

aspect CatchSystemPrinting {
    pointcut scope(): within(com.example..*);
    declare warning: scope() && 
        (get(* System.out) || get(* System.err) || 
        call(* Throwable.printStackTrace())):
        "Don't print, use the logger";
}

In many cases you can use existing library functionality to redirect output to a log using System.setOut and System.setErr. See, for example, http://www.mail-archive.com/log4j-user@xxxxxxxxxxxxxxxxxx/msg08224.html for Log4J. This is also helpful when you don't want to weave into 3rd party code (e.g., libraries).

Ron

Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895

> ------------Original Message-------------
> From: "Costakos,David" <costakod@xxxxxxxx>
> To: "'aspectj-users@xxxxxxxxxxx'" <aspectj-users@xxxxxxxxxxx>
> Date: Mon, Jul-28-2003 9:10 AM
> Subject: RE: [aspectj-users] around() Problem
> 
> Someone once posted some code on how to do this which I shamelessly copied
> and implemented -- thanks to whomever that was.
> 
> Here is an example:
> 
> /*----------- Pointcuts --------------------*/
>   // There may be some methods, like "main" methods,
>   // that you want to ignore.
>   protected abstract pointcut ignorable();
> 
>   // catch PrintStream.print messages
>   pointcut sysIO(PrintStream out, Object message):
>     call(void PrintStream.print*(..)) &&
>     target(out) && args(message) && !ignorable();
> 
>   // its unlikely but possible that someone will call
>   // System.out.println() with no arguments -- let's ignore that
>   pointcut sysIONL(PrintStream out):
>     call(void PrintStream.println()) && 
>     target(out) && !ignorable();
> 
>   // Catch Exception Stack Trace Prints
>   pointcut stackTrace(Throwable t):
>     call(void Throwable.printStackTrace()) && 
>     target(t) && !ignorable();
> 
> /*----------- Advice --------------------------*/
>   void around(PrintStream out, Object message): sysIO(out, message) {
>     if((out == System.out) || (out == System.err))
>       logger.info(message);
>     else
>       proceed(out, message);
>   }
> 
>   void around(PrintStream out): sysIONL(out) {
>     if (out == System.out || out == System.err)
>       ; // do nothing
>     else
>       proceed(out);
>   }
> 
>   void around(Throwable t): stackTrace(t) {
>     logger.warn("Caught Stack Trace Print from code", t);
>   }
> 
> 
> -----Original Message-----
> From: Michael Dempfle [mailto:mdempfle@xxxxxxxxxx]
> Sent: Monday, July 28, 2003 11:40 AM
> To: aspectj-users@xxxxxxxxxxx
> Subject: [aspectj-users] around() Problem
> 
> 
> Hi,
> 
> I've tried now some time to catch all System.outs in my code and can't 
> make it.
> 
> In the end I want to catch alle the outputs in an existing application 
> and want to write it
> to a logfile.
> 
> But for testing I simply want to catch all System.outs with this code:
> 
> public aspect SystemOut {
>     pointcut pprint(String str) :
>         call(void System.out.println(*)) && target(str) ;
> 
>     void around(String str) : pprint(str) {
>         System.out.println("Catched in " + thisJoinPoint + ": " + str);
>         proceed(str);
>     }
> }
> 
> And I always get the message: no match for this type name
> But I extra put in this line of code to test: System.out.println("Test");
> 
> What I am doing wrong?
> Is there a problem because System.out.println exists for a lot of objects?
> I want to catch them independent if it's called with an int or a String 
> or ....
> 
> Thanks,
> Michael
> 
> 
> 
> 
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 


Back to the top