ij-16.png   inspectorJ -- JavaTM Profiler
sf project site browse source checkout source
SourceForge.net Logo



src/inspectorj/agent/agentutils.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   inspectorJ - java profiler                                            *
00003  *   Copyright (C) 2007 by James May
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00027 #ifndef AGENT_UTILS_H
00028 #define AGENT_UTILS_H
00029 
00030 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00031 #  if !defined(WIN32_LEAN_AND_MEAN)
00032 #   define WIN32_LEAN_AND_MEAN
00033 #  endif // !defined(WIN32_LEAN_AND_MEAN)
00034 #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
00035         
00036 #include <iostream>        
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <stddef.h>
00041 #include <stdarg.h>
00042 #include <ctime>
00043 #include <QString>
00044 #include <QStringList>
00045 #include <QStringListIterator>
00046 #include <QList>
00047 #include <QRegExp>
00048 #include "jvmti.h"
00049 
00050 
00051 char *getToken(char *str, char *seps, char *buf, int max);
00052 bool coveredByList(char *cname, char *mname, QList<QRegExp>*);
00053 bool interested(char *cname, char *mname, QList<QRegExp>*);
00054 void createAgentClassFilter(QRegExp &regexp, QString filterString);
00055                                 
00056 namespace inspectorj {
00057 namespace agent {
00058             
00063 class AgentLogger
00064 {
00065     public: 
00066                 
00067         static int loglevel;
00068         
00074         static void
00075         print(const char * format, ...)
00076         {
00077             va_list ap;
00078         
00079             va_start(ap, format);
00080             (void)vfprintf(stdout, format, ap);
00081             va_end(ap);
00082             (void)fflush(stdout);
00083         }     
00084            
00090         static void
00091         println(const char * format, ...)
00092         {
00093             va_list ap;
00094         
00095             va_start(ap, format);
00096             (void)vfprintf(stdout, format, ap);
00097             va_end(ap);
00098             fprintf(stdout, "\n");
00099             (void)fflush(stdout);
00100         }         
00101         
00106         static void
00107         debug(const char * format, ...)
00108         {
00109             if (loglevel < 99) return;
00110             va_list ap;
00111 
00112             printTimeString(stdout);
00113             fprintf(stdout, " [DEBUG] ");
00114             va_start(ap, format);
00115             (void)vfprintf(stdout, format, ap);
00116             va_end(ap);
00117             fprintf(stdout, "\n");
00118             (void)fflush(stdout);              
00119 
00120         }        
00121         
00126         static void
00127         info(const char * format, ...)
00128         {
00129             if (loglevel < 2) return;
00130             va_list ap;
00131             printTimeString(stdout);
00132             fprintf(stdout, " [INFO] ");
00133             va_start(ap, format);
00134             (void)vfprintf(stdout, format, ap);
00135             va_end(ap);
00136             fprintf(stdout, "\n");
00137             (void)fflush(stdout);            
00138         }
00139         
00143         static void
00144         warn(const char * format, ...)
00145         {
00146             if (loglevel < 1) return;
00147             va_list ap;
00148         
00149             printTimeString(stdout);
00150             fprintf(stdout, " [WARN] ");            
00151             va_start(ap, format);
00152             (void)vfprintf(stdout, format, ap);
00153             va_end(ap);
00154             fprintf(stdout, "\n");
00155             (void)fflush(stdout);              
00156         }        
00157         
00161         static void
00162         error(const char * format, ...)
00163         {
00164             va_list ap;
00165         
00166             printTimeString(stderr);
00167             fprintf(stderr, " [ERROR] ");             
00168             va_start(ap, format);
00169             (void)vfprintf(stderr, format, ap);
00170             fprintf(stderr, "\n");            
00171             (void)fflush(stderr);
00172             va_end(ap);
00173         }  
00174          
00178         static void
00179         fatal(const char * format, ...)
00180         {
00181             va_list ap;
00182         
00183             printTimeString(stderr);
00184             fprintf(stderr, " [FATAL] ");             
00185             va_start(ap, format);
00186             (void)vfprintf(stderr, format, ap);
00187             fprintf(stderr, "\n");            
00188             (void)fflush(stderr);
00189             va_end(ap);
00190             exit(3);
00191         }  
00192                        
00196         static char*
00197         toChar(const QString& qstr)
00198         {
00199             return qstr.toLocal8Bit().data();
00200         }
00201                 
00202         /* Convenience wrappers */
00203         static void print (const QString qstr, ...) { va_list va; print(toChar(qstr),va); } 
00204         static void println (const QString qstr, ...) { va_list va; println(toChar(qstr),va); }
00205         static void debug (const QString qstr, ...) { va_list va; debug(toChar(qstr),va); }
00206         static void info (const QString qstr, ...) { va_list va; info(toChar(qstr),va); }
00207         static void warn (const QString qstr, ...) { va_list va; warn(toChar(qstr),va); }
00208         static void error (const QString qstr, ...) { va_list va; error(toChar(qstr),va); }
00209         static void fatal (const QString qstr, ...) { va_list va; fatal(toChar(qstr),va); }
00210              
00211     private:
00212         
00217         static void printTimeString(FILE * stream)
00218         {
00219             using namespace std;
00220             time_t now = time(0);
00221             tm *timer = localtime (&now);
00222 
00223             char tmp[24];
00224             char* format = "%Y-%m-%d %I:%M:%S -";
00225             size_t cnt = strftime(tmp, 24, format, timer);
00226             fprintf(stream, tmp);
00227         }                
00228 };
00229 
00230 } // end namespace agent
00231 } // end namespace inspectorj
00232 
00233 
00234 /* Evaluate JVMTI error code */
00235 static void
00236 check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str)
00237 {
00238     if ( errnum != JVMTI_ERROR_NONE ) {
00239     char       *errnum_str;
00240 
00241     errnum_str = NULL;
00242     (void)jvmti->GetErrorName(errnum, &errnum_str);
00243 
00244     inspectorj::agent::AgentLogger::error("ERROR: JVMTI: %d(%s): %s", errnum,
00245         (errnum_str==NULL?"Unknown":errnum_str),
00246         (str==NULL?"":str));
00247     }
00248 }
00249 
00250 #endif // end AGENT_UTILS_H
00251 

Generated on Sun Aug 19 17:07:53 2007 for inspectorJ by  doxygen 1.5.1