LLVM API Documentation

SlowOperationInformer.cpp

Go to the documentation of this file.
00001 //===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SlowOperationInformer class for the LLVM debugger.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/SlowOperationInformer.h"
00015 #include "llvm/System/IncludeFile.h"
00016 #include "llvm/System/Alarm.h"
00017 #include <iostream>
00018 #include <sstream>
00019 #include <cassert>
00020 using namespace llvm;
00021 
00022 SlowOperationInformer::SlowOperationInformer(const std::string &Name)
00023   : OperationName(Name), LastPrintAmount(0) {
00024   sys::SetupAlarm(1);
00025 }
00026 
00027 SlowOperationInformer::~SlowOperationInformer() {
00028   sys::TerminateAlarm();
00029   if (LastPrintAmount) {
00030     // If we have printed something, make _sure_ we print the 100% amount, and
00031     // also print a newline.
00032     std::cout << std::string(LastPrintAmount, '\b') << "Progress "
00033               << OperationName << ": 100%  \n";
00034   }
00035 }
00036 
00037 /// progress - Clients should periodically call this method when they are in
00038 /// an exception-safe state.  The Amount variable should indicate how far
00039 /// along the operation is, given in 1/10ths of a percent (in other words,
00040 /// Amount should range from 0 to 1000).
00041 bool SlowOperationInformer::progress(unsigned Amount) {
00042   int status = sys::AlarmStatus();
00043   if (status == -1) {
00044     std::cout << "\n";
00045     LastPrintAmount = 0;
00046     return true;
00047   }
00048 
00049   // If we haven't spent enough time in this operation to warrant displaying the
00050   // progress bar, don't do so yet.
00051   if (status == 0)
00052     return false;
00053 
00054   // Delete whatever we printed last time.
00055   std::string ToPrint = std::string(LastPrintAmount, '\b');
00056 
00057   std::ostringstream OS;
00058   OS << "Progress " << OperationName << ": " << Amount/10;
00059   if (unsigned Rem = Amount % 10)
00060     OS << "." << Rem << "%";
00061   else
00062     OS << "%  ";
00063 
00064   LastPrintAmount = OS.str().size();
00065   std::cout << ToPrint+OS.str() << std::flush;
00066   return false;
00067 }
00068 
00069 DEFINING_FILE_FOR(SupportSlowOperationInformer)