001    // Copyright 2008 Waterken Inc. under the terms of the MIT X license
002    // found at http://www.opensource.org/licenses/mit-license.html
003    package org.waterken.trace;
004    
005    import java.lang.reflect.Method;
006    import java.lang.reflect.Modifier;
007    
008    import org.joe_e.reflect.Reflection;
009    import org.ref_send.log.Comment;
010    import org.ref_send.log.Event;
011    import org.ref_send.log.Fulfilled;
012    import org.ref_send.log.Got;
013    import org.ref_send.log.Pipelined;
014    import org.ref_send.log.Problem;
015    import org.ref_send.log.Progressed;
016    import org.ref_send.log.Rejected;
017    import org.ref_send.log.Resolved;
018    import org.ref_send.log.Returned;
019    import org.ref_send.log.Sent;
020    import org.ref_send.log.SentIf;
021    import org.ref_send.promise.Log;
022    import org.ref_send.promise.Receiver;
023    
024    /**
025     * Event logging infrastructure.
026     */
027    public final class
028    EventSender {
029        private EventSender() {}
030        
031        /**
032         * Constructs a log event generator.
033         * @param stderr    log event output factory
034         * @param mark      event counter
035         * @param tracer    stack tracer
036         */
037        static public Log
038        make(final Receiver<Event> stderr, final Marker mark, final Tracer tracer) {
039            class LogX extends Log {
040                static private final long serialVersionUID = 1L;
041    
042                public @Override void
043                comment(final String text) {
044                    stderr.apply(new Comment(mark.apply(), tracer.timestamp(),
045                                             tracer.traceHere(), text));
046                }
047                
048                public @Override void
049                problem(final Exception reason) {
050                    stderr.apply(new Problem(mark.apply(), tracer.timestamp(),
051                                             tracer.traceException(reason),
052                                             tracer.readException(reason), reason));
053                }
054    
055                public @Override void
056                got(final String message, final Class<?> concrete, Method method) {
057                    if (null != concrete && null != method &&
058                            !Modifier.isStatic(method.getModifiers())) {
059                        try {
060                            method = Reflection.method(concrete, method.getName(),
061                                                       method.getParameterTypes());
062                        } catch (final NoSuchMethodException e) {
063                            // Log abstract method if concrete one cannot be found.
064                        }
065                    }
066                    stderr.apply(new Got(mark.apply(), tracer.timestamp(),
067                        null!=method ? tracer.traceMember(method) : null, message));
068                }
069    
070                public @Override void
071                sent(final String message) {
072                    stderr.apply(new Sent(mark.apply(), tracer.timestamp(),
073                                          tracer.traceHere(), message));
074                }
075    
076                public @Override void
077                returned(final String message) {
078                    stderr.apply(new Returned(mark.apply(), tracer.timestamp(),
079                                              null, message));
080                }
081    
082                public @Override void
083                sentIf(final boolean pipelined,
084                       final String message, final String condition) {
085                    if (pipelined) {
086                        stderr.apply(new Pipelined(mark.apply(), tracer.timestamp(),
087                                                                       tracer.traceHere(),
088                                                                       message, condition));
089                    } else {
090                            stderr.apply(new SentIf(mark.apply(), tracer.timestamp(),
091                                                                tracer.traceHere(),
092                                                                message, condition));
093                    }
094                }
095    
096                public @Override void
097                resolved(final String condition) {
098                    stderr.apply(new Resolved(mark.apply(), tracer.timestamp(),
099                                              tracer.traceHere(), condition));
100                }
101    
102                public @Override void
103                fulfilled(final String condition) {
104                    stderr.apply(new Fulfilled(mark.apply(), tracer.timestamp(),
105                                               tracer.traceHere(), condition));
106                }
107                
108                public @Override void
109                rejected(final String condition, final Exception reason) {
110                    stderr.apply(new Rejected(mark.apply(), tracer.timestamp(),
111                                              tracer.traceHere(),condition,reason));
112                }
113                
114                public @Override void
115                progressed(final String condition) {
116                    stderr.apply(new Progressed(mark.apply(), tracer.timestamp(),
117                                                tracer.traceHere(), condition));
118                }
119            }
120            return new LogX();
121        }
122    }