2003-12-31 13:40:02 +08:00
|
|
|
//===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-12-31 13:40:02 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-12-31 13:40:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-12-31 13:40:02 +08:00
|
|
|
// This file implements the SlowOperationInformer class for the LLVM debugger.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/SlowOperationInformer.h"
|
2006-11-26 18:52:51 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2005-12-22 11:31:26 +08:00
|
|
|
#include "llvm/System/Alarm.h"
|
2003-12-31 13:40:02 +08:00
|
|
|
#include <sstream>
|
2003-12-31 15:31:10 +08:00
|
|
|
#include <cassert>
|
2003-12-31 13:40:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
SlowOperationInformer::SlowOperationInformer(const std::string &Name)
|
|
|
|
: OperationName(Name), LastPrintAmount(0) {
|
2005-12-22 11:31:26 +08:00
|
|
|
sys::SetupAlarm(1);
|
2003-12-31 13:40:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SlowOperationInformer::~SlowOperationInformer() {
|
2005-12-22 11:31:26 +08:00
|
|
|
sys::TerminateAlarm();
|
2003-12-31 18:20:38 +08:00
|
|
|
if (LastPrintAmount) {
|
|
|
|
// If we have printed something, make _sure_ we print the 100% amount, and
|
|
|
|
// also print a newline.
|
2006-12-07 09:30:32 +08:00
|
|
|
cout << std::string(LastPrintAmount, '\b') << "Progress "
|
|
|
|
<< OperationName << ": 100% \n";
|
2003-12-31 18:20:38 +08:00
|
|
|
}
|
2003-12-31 13:40:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// progress - Clients should periodically call this method when they are in
|
|
|
|
/// an exception-safe state. The Amount variable should indicate how far
|
|
|
|
/// along the operation is, given in 1/10ths of a percent (in other words,
|
|
|
|
/// Amount should range from 0 to 1000).
|
2006-07-07 06:34:06 +08:00
|
|
|
bool SlowOperationInformer::progress(unsigned Amount) {
|
2005-12-22 11:31:26 +08:00
|
|
|
int status = sys::AlarmStatus();
|
|
|
|
if (status == -1) {
|
2006-12-07 09:30:32 +08:00
|
|
|
cout << "\n";
|
2003-12-31 13:40:02 +08:00
|
|
|
LastPrintAmount = 0;
|
2006-07-07 06:34:06 +08:00
|
|
|
return true;
|
2003-12-31 13:40:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't spent enough time in this operation to warrant displaying the
|
|
|
|
// progress bar, don't do so yet.
|
2005-12-22 11:31:26 +08:00
|
|
|
if (status == 0)
|
2006-07-07 06:34:06 +08:00
|
|
|
return false;
|
2003-12-31 13:40:02 +08:00
|
|
|
|
|
|
|
// Delete whatever we printed last time.
|
|
|
|
std::string ToPrint = std::string(LastPrintAmount, '\b');
|
|
|
|
|
|
|
|
std::ostringstream OS;
|
2003-12-31 18:20:38 +08:00
|
|
|
OS << "Progress " << OperationName << ": " << Amount/10;
|
|
|
|
if (unsigned Rem = Amount % 10)
|
|
|
|
OS << "." << Rem << "%";
|
|
|
|
else
|
|
|
|
OS << "% ";
|
2003-12-31 13:40:02 +08:00
|
|
|
|
|
|
|
LastPrintAmount = OS.str().size();
|
2006-12-07 09:30:32 +08:00
|
|
|
cout << ToPrint+OS.str() << std::flush;
|
2006-07-07 06:34:06 +08:00
|
|
|
return false;
|
2003-12-31 13:40:02 +08:00
|
|
|
}
|