Home | History | Annotate | Download | only in tests
      1 /*
      2 **
      3 ** Copyright 2015, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #define LOG_TAG "perfprofd"
     19 
     20 #include <stdarg.h>
     21 #include <unistd.h>
     22 #include <vector>
     23 #include <string>
     24 #include <assert.h>
     25 
     26 #include <utils/Log.h>
     27 
     28 #include "perfprofdutils.h"
     29 
     30 static std::vector<std::string> *mock_log;
     31 
     32 static void append_to_log(const std::string &s)
     33 {
     34   assert(mock_log);
     35   mock_log->push_back(s);
     36 }
     37 
     38 void mock_perfprofdutils_init()
     39 {
     40   assert(!mock_log);
     41   mock_log = new std::vector<std::string>;
     42 }
     43 
     44 void mock_perfprofdutils_finish()
     45 {
     46   assert(mock_log);
     47   delete mock_log;
     48 }
     49 
     50 std::string mock_perfprofdutils_getlogged()
     51 {
     52   std::string result;
     53   assert(mock_log);
     54   for (const std::string &s : (*mock_log)) {
     55     result += s;
     56   }
     57   mock_log->clear();
     58   return result;
     59 }
     60 
     61 extern "C" {
     62 
     63 #define LMAX 8192
     64 
     65 void perfprofd_mocklog(const char *tag, const char *fmt, va_list ap)
     66 {
     67     char buffer[LMAX];
     68     strcpy(buffer, tag);
     69     vsnprintf(buffer+strlen(tag), LMAX, fmt, ap);
     70     std::string b(buffer); b += "\012";
     71     append_to_log(b);
     72 }
     73 
     74 void perfprofd_log_error(const char *fmt, ...)
     75 {
     76     va_list ap;
     77     va_start(ap, fmt);
     78     vfprintf(stderr, fmt, ap); fprintf(stderr, "\n");
     79     perfprofd_mocklog("E: ", fmt, ap);
     80     va_end(ap);
     81 }
     82 
     83 void perfprofd_log_warning(const char *fmt, ...)
     84 {
     85     va_list ap;
     86     va_start(ap, fmt);
     87     vfprintf(stderr, fmt, ap); fprintf(stderr, "\n");
     88     perfprofd_mocklog("W: ", fmt, ap);
     89     va_end(ap);
     90 }
     91 
     92 void perfprofd_log_info(const char *fmt, ...)
     93 {
     94     va_list ap;
     95     va_start(ap, fmt);
     96     vfprintf(stderr, fmt, ap); fprintf(stderr, "\n");
     97     perfprofd_mocklog("I: ", fmt, ap);
     98     va_end(ap);
     99 }
    100 
    101 void perfprofd_sleep(int seconds)
    102 {
    103     perfprofd_log_info("sleep %d seconds", seconds);
    104 }
    105 
    106 }
    107