1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// File:    CubitInstrumentation.cpp<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
// Author:  Boyd Tidwell
//
// Purpose: CubitInstrumentation is used to collect and write command and 
//          feature usage by Cubit users. 

#include "CubitInstrumentation.hpp"
#include "CubitMessage.hpp"


//#include <iostream>

CubitInstrumentation* CubitInstrumentation::thisInstance = NULL;

//=============================================================================
CubitInstrumentation* CubitInstrumentation::instance()
{
  if (!thisInstance)
  {
    thisInstance = new CubitInstrumentation;
    if (!thisInstance)
    {
      PRINT_ERROR("Unable to instantiate instrumentation object!\n");
      exit(1);
    }
  }
  return thisInstance;
}

//=============================================================================
void CubitInstrumentation::delete_instance()
{
  if (thisInstance)
    delete thisInstance;
  thisInstance = NULL;
}

//=============================================================================
CubitInstrumentation::CubitInstrumentation()
  : outputState(Unknown), 
    recordTokens(true), validKeywords(false), writeKeywords(true),
    tokenUsageStream(NULL)
{}

//=============================================================================
CubitInstrumentation::~CubitInstrumentation()
{
    // close the token file
  if (tokenUsageStream) 
  {
    close_section();
    tokenUsageStream->close();
    delete tokenUsageStream;
  }
  thisInstance = NULL;
}

//=============================================================================
void CubitInstrumentation::write_context_token(int token)
{<--- The function 'write_context_token' is never used.
  if (check_token_log())
  {
    if (outputState != GUI)
    {
      outputState = open_section(GUI);
    }
    else
    {
      *tokenUsageStream << ",";
    }
    *tokenUsageStream << token;
    tokenUsageStream->flush();
  }
}

//=============================================================================
void CubitInstrumentation::write_all_words()
{
  if (tokenUsageStream && !keywordMap.empty()) {
    if (outputState != Keyword)
    {
      outputState = open_section(Keyword);
    }

    std::map<std::string, int>::iterator it = keywordMap.begin();
    std::map<std::string, int>::iterator end = keywordMap.end();
    *tokenUsageStream << keywordMap.size() << std::endl;
    *tokenUsageStream << (*it).first;
    ++it;
    for ( ; it != end; it++)
    {<--- Prefer prefix ++/-- operators for non-primitive types.
      *tokenUsageStream << "," << (*it).first;
    }
    close_section();
    tokenUsageStream->flush();
  }
}

//=============================================================================
void CubitInstrumentation::load_all_words(const char* const allwords[])
{<--- The function 'load_all_words' is never used.
  assert(keywordMap.size() == 0);
<--- Possible inefficient checking for 'keywordMap' emptiness.
  int index = 0;
  while (allwords[index] != NULL) {
    keywordMap[allwords[index]] = index + 1;
    ++index;
  }

  write_all_words();
  validKeywords = true;
}

//=============================================================================
int CubitInstrumentation::lookup_keyword(const char* keyword)
{
  if (keyword == NULL)
    return -1;

  std::map<std::string, int>::iterator it = keywordMap.find(keyword);
  if (it != keywordMap.end())
    return (*it).second;

  return -1;
}

//=============================================================================
void CubitInstrumentation::write_keywords(std::vector<CubitString> keywords)
{<--- The function 'write_keywords' is never used.
  if (check_token_log() && !keywords.empty())
  {
    if (outputState != Command)
    {
      outputState = open_section(Command);
    }
    else
    {
      *tokenUsageStream << ",";
    }
    *tokenUsageStream << lookup_keyword(keywords[0].c_str());
    for (size_t i = 1; i < keywords.size(); i++)
    {
      int debug = lookup_keyword(keywords[i].c_str());
      *tokenUsageStream << ":" << debug;
    }
    tokenUsageStream->flush();
  }
}

//=============================================================================
void CubitInstrumentation::close_section()
{
  if (outputState == Command)
    *tokenUsageStream << std::endl << "</command>" << std::endl;
  else if (outputState == GUI)
    *tokenUsageStream << std::endl << "</gui>" << std::endl;
  else if (outputState == Keyword)
    *tokenUsageStream << std::endl << "</keyword>" << std::endl;
 
  outputState = Unknown;
}

//=============================================================================
CubitInstrumentation::SectionState
CubitInstrumentation::open_section(SectionState state)
{
  close_section();

  if (state == Command)
    *tokenUsageStream << "<command>" << std::endl;
  else if (state == GUI)
    *tokenUsageStream << "<gui>" << std::endl;
  else if (state == Keyword)
    *tokenUsageStream << "<keyword>" << std::endl;
  else
    return Unknown;

  return state;
}

//=============================================================================
bool CubitInstrumentation::check_token_log()
{
  const char* token_file = getenv("_CUBIT_USAGE_TOKEN_FILE");
  if (token_file)
  {
    if (!tokenUsageStream)
    {
      //const char* token_file = getenv("_CUBIT_USAGE_TOKEN_FILE");
      //if(!token_file)
      //  token_file = "usage_tokens.log";
      tokenUsageStream = new std::ofstream(token_file, std::ofstream::out);
      if (!tokenUsageStream)
      {
        std::string file_name = getenv("_CUBIT_USAGE_TOKEN_DIR");
        file_name += std::tmpnam(NULL);
        file_name += "log";
        tokenUsageStream = new std::ofstream(file_name.c_str(), std::ofstream::out);
        if (!tokenUsageStream)
        {
          PRINT_ERROR("Failed to open token usage file!\n");
          PRINT_INFO("\tNo usage token will be recorded.\n");
          return false;
        }
      }
    }

      // write keywords if valid and write flags set
    if (validKeywords && writeKeywords)
    {
      write_all_words();
      writeKeywords = false;
    }
    return true; 
  }
  else
  {
    return false;
  }
}